Here you can find the source of writeBytesToFile(File theFile, byte[] bytes)
Parameter | Description |
---|---|
theFile | File Object representing the path to write to. |
bytes | The byte[] of data to write to the File. |
Parameter | Description |
---|---|
IOException | Thrown if there is problem creating or writing the File. |
public static void writeBytesToFile(File theFile, byte[] bytes) throws IOException
//package com.java2s; /************************************************************************************** Copyright 2015 Applied Research Associates, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at:/* w w w. jav a 2 s .c om*/ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. **************************************************************************************/ import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class Main { /** * Writes the specified byte[] to the specified File path. * * @param theFile File Object representing the path to write to. * @param bytes The byte[] of data to write to the File. * @throws IOException Thrown if there is problem creating or writing the * File. */ public static void writeBytesToFile(File theFile, byte[] bytes) throws IOException { BufferedOutputStream bos = null; try { FileOutputStream fos = new FileOutputStream(theFile); bos = new BufferedOutputStream(fos); bos.write(bytes); } finally { if (bos != null) { try { //flush and close the BufferedOutputStream bos.flush(); bos.close(); } catch (Exception e) { } } } } }