Java FileOutputStream Write saveToFile(byte[] data, String filename)

Here you can find the source of saveToFile(byte[] data, String filename)

Description

save To File

License

Open Source License

Declaration

public static boolean saveToFile(byte[] data, String filename) throws IOException 

Method Source Code

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

import java.io.ByteArrayInputStream;

import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    public static boolean saveToFile(byte[] data, String filename) throws IOException {
        OutputStream os = null;/*ww w.  j  a v a2 s.  c om*/
        ByteArrayInputStream bin = null;
        try {

            if (data != null) {
                bin = new ByteArrayInputStream(data);
                os = new FileOutputStream(filename);
                byte[] bytes = new byte[1024];
                int c;
                while ((c = bin.read(bytes)) != -1) {
                    os.write(bytes, 0, c);
                }
                os.flush();
                return true;
            }
            ;
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                if (os != null) {
                    os.close();
                    os = null;
                }
            } catch (Exception e) {

            }
            try {
                if (bin != null) {
                    bin.close();
                    bin = null;
                }
            } catch (Exception e) {

            }
        }
        return false;
    }

    public static boolean saveToFile(InputStream ins, String filename) throws IOException {
        try {
            OutputStream out = new FileOutputStream(new File(filename));
            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = ins.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            out.flush();
            out.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}

Related

  1. saveToBinaryFile(File dest, byte[] data)
  2. saveToDisc(final InputStream fileInputStream, final String fileUploadPath)
  3. saveToDisk(InputStream is, String filename)
  4. saveToDisk(String contents, String filename)
  5. saveToEml(Message mail, File emlFile)
  6. saveToFile(File file, byte[] buffer)
  7. saveToFile(final String file, final byte[] data, final boolean overwrite)
  8. saveToFile(final String text, final File file)
  9. saveToFile(InputStream in, File dst)