Android Byte Array to File Save byteArrayToFile(byte[] bytes, String filePath)

Here you can find the source of byteArrayToFile(byte[] bytes, String filePath)

Description

byte Array To File

Declaration

public static void byteArrayToFile(byte[] bytes, String filePath)
        throws Exception 

Method Source Code

//package com.java2s;
import java.io.ByteArrayInputStream;

import java.io.File;

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

public class Main {

    private static final int CACHE_SIZE = 1024;

    public static void byteArrayToFile(byte[] bytes, String filePath)
            throws Exception {
        InputStream in = new ByteArrayInputStream(bytes);
        File destFile = new File(filePath);
        if (!destFile.getParentFile().exists()) {
            destFile.getParentFile().mkdirs();
        }/*from  w w  w. j a va 2s .c o  m*/
        destFile.createNewFile();
        OutputStream out = new FileOutputStream(destFile);
        byte[] cache = new byte[CACHE_SIZE];
        int nRead = 0;
        while ((nRead = in.read(cache)) != -1) {
            out.write(cache, 0, nRead);
            out.flush();
        }
        out.close();
        in.close();
    }
}

Related

  1. bytesToFile(byte[] bytes, String filePath)
  2. byte2file(byte[] data, String path)