Java Write Byte Array to File writeFile(byte data[], File file)

Here you can find the source of writeFile(byte data[], File file)

Description

Writes a byte array to the specified file

License

Apache License

Parameter

Parameter Description
data The byte array to write to the file
file The file to which the byte array is written

Exception

Parameter Description
IOException if an error occurred.

Return

True if the bytes were successfully written to the file

Declaration

public static boolean writeFile(byte data[], File file) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

public class Main {
    /**/*from   www  .j a  v a  2 s . c om*/
     *** Writes a byte array to the specified file
     *** 
     * @param data
     *            The byte array to write to the file
     *** @param file
     *            The file to which the byte array is written
     *** @return True if the bytes were successfully written to the file
     *** @throws IOException
     *             if an error occurred.
     **/
    public static boolean writeFile(byte data[], File file) throws IOException {
        return writeFile(data, file, false);
    }

    /**
     *** Writes a byte array to the specified file
     *** 
     * @param data
     *            The byte array to write to the file
     *** @param file
     *            The file to which the byte array is written
     *** @param append
     *            True to append the bytes to the file, false to overwrite.
     *** @return True if the bytes were successfully written to the file
     *** @throws IOException
     *             if an error occurred.
     **/
    public static boolean writeFile(byte data[], File file, boolean append) throws IOException {
        if ((data != null) && (file != null)) {
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(file, append);
                fos.write(data, 0, data.length);
                return true;
            } finally {
                try {
                    fos.close();
                } catch (Throwable t) {/* ignore */
                }
            }
        }
        return false;
    }
}

Related

  1. writeFile(byte[] b, String fileName)
  2. writeFile(byte[] buf, File file)
  3. writeFile(byte[] bytes, File file)
  4. writeFile(byte[] bytes, File file)