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

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

Description

bytes To File

Declaration

public static void bytesToFile(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 bytesToFile(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  ww  .ja  v a  2s.  com
        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. byteArrayToFile(byte[] bytes, String filePath)
  2. byte2file(byte[] data, String path)