Java FileOutputStream Write Byte Array writeBytesToFile(File theFile, byte[] bytes)

Here you can find the source of writeBytesToFile(File theFile, byte[] bytes)

Description

Writes the specified byte[] to the specified File path.

License

Apache License

Parameter

Parameter Description
theFile File Object representing the path to write to.
bytes The byte[] of data to write to the File.

Exception

Parameter Description
IOException Thrown if there is problem creating or writing the File.

Declaration

public static void writeBytesToFile(File theFile, byte[] bytes) throws IOException 

Method Source Code

//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) {
                }
            }
        }
    }
}

Related

  1. writeBytesToFile(byte[] bytes, File file)
  2. writeBytestoFile(byte[] bytes, String filepath)
  3. writeBytesToFile(byte[] data, String path)
  4. writeBytesToFile(File destination, byte[] bytes)
  5. writeBytesToFile(File file, byte[] bytes)
  6. writeBytesToFile(final byte[] data, String filename)
  7. writeBytesToFile(List bytes, File file)
  8. writeBytesToFile(String filename, byte[] data)
  9. writeBytesToFile(String strFilePath, byte[] fileData)