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

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

Description

Takes a byte array, or list of byte arrays, and writes them into a file.

License

Apache License

Parameter

Parameter Description
file - The file to write in.
data - The byte array, or list of byte arrays to write.

Declaration

public static void writeFile(File file, byte[]... data) 

Method Source Code

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

import java.io.BufferedOutputStream;
import java.io.File;

import java.io.FileOutputStream;

public class Main {
    /**//from w w w  . j a  va2  s.co  m
     * Takes a byte array, or list of byte arrays, and writes them into a file. This method replaces any current data, with the new
     * data provided by the byte array. Using a list of byte arrays will combine the bytes arrays into a single byte array as it writes
     * them.
     * @param file - The file to write in.
     * @param data - The byte array, or list of byte arrays to write.
     */
    public static void writeFile(File file, byte[]... data) {
        if (data == null)
            return;
        BufferedOutputStream out = null;
        try {
            out = new BufferedOutputStream(new FileOutputStream(file));
            for (byte[] d : data)
                out.write(d);
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        }
    }
}

Related

  1. writeFile(File file, byte[] data)
  2. writeFile(File file, byte[] data)
  3. writeFile(File file, byte[] data)
  4. writeFile(File file, byte[] data)
  5. writeFile(File file, byte[] data)
  6. writeFile(final File file, byte[] data)
  7. writeFile(String filename, byte data[])
  8. writeFile(String filename, byte[] bs)
  9. writeFile(String fileName, byte[] bytes)