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

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

Description

write File

License

Apache License

Declaration

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

Method Source Code

//package com.java2s;
/*#######################################################
 *
 *   Maintained by Gregor Santner, 2017-
 *   https://gsantner.net//*w w w. j  a va  2  s.  c  om*/
 *
 *   License: Apache 2.0
 *  https://github.com/gsantner/opoc/#licensing
 *  https://www.apache.org/licenses/LICENSE-2.0
 *
#########################################################*/

import java.io.BufferedOutputStream;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileOutputStream;

import java.io.FileWriter;
import java.io.IOException;

import java.io.OutputStream;

public class Main {
    public static boolean writeFile(final File file, byte[] data) {
        try {
            OutputStream output = null;
            try {
                output = new BufferedOutputStream(new FileOutputStream(file));
                output.write(data);
                output.flush();
                return true;
            } finally {
                if (output != null) {
                    output.close();
                }
            }
        } catch (Exception ex) {
            return false;
        }
    }

    public static boolean writeFile(final File file, final String content) {
        BufferedWriter writer = null;
        try {
            if (!file.getParentFile().isDirectory() && !file.getParentFile().mkdirs())
                return false;

            writer = new BufferedWriter(new FileWriter(file));
            writer.write(content);
            writer.flush();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.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(String filename, byte data[])
  7. writeFile(String filename, byte[] bs)
  8. writeFile(String fileName, byte[] bytes)
  9. writeFile(String fileName, byte[] bytes)