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

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

Description

write File

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedWriter;
import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;
import java.io.OutputStreamWriter;

import java.util.zip.GZIPOutputStream;

public class Main {
    public synchronized static void writeFile(File filename, String data, boolean gzip) throws IOException {
        OutputStream outputStream = null;

        try {/*from  w w  w .j  a  v  a2s .com*/
            outputStream = new FileOutputStream(filename);

            if (gzip) {
                outputStream = new GZIPOutputStream(outputStream);
            }

            try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"))) {
                writer.write(data);
            }
        } finally {
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }

    public synchronized static void writeFile(File file, byte[] data) throws IOException {
        file.mkdirs();

        try (FileOutputStream fos = new FileOutputStream(file)) {
            fos.write(data);
        }

    }

    public synchronized static void writeFile(File filename, String data) throws IOException {
        writeFile(filename, data, false);
    }
}

Related

  1. writeFile(File file, byte[] context, boolean append)
  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(File file, byte[] data)
  7. writeFile(File file, byte[] data)
  8. writeFile(File file, byte[]... data)
  9. writeFile(final File file, byte[] data)