Java将bytes []转换为File

时间:2014-05-08 05:49:43

标签: java file byte

我有一个字节数组

bytes[] doc

如何从该字节创建new File的实例?

3 个答案:

答案 0 :(得分:17)

 try (FileOutputStream fileOuputStream = new FileOutputStream("filename")){
    fileOuputStream.write(byteArray);
 }

答案 1 :(得分:11)

试试这个

Files.write(Paths.get("filename"), bytes);

答案 2 :(得分:4)

一个将byte []写入文件的简单程序。

import java.io.File;
import java.io.FileOutputStream;

public class BytesToFile {

public static void main(String[] args) {

    byte[] demBytes = null; //instead of null, specify your bytes here. 

    File outputFile = new File("LOCATION TO FILE");

    try ( FileOutputStream outputStream = new FileOutputStream(outputFile); ) {

        outputStream.write(demBytes);  //write the bytes and your done. 

    } catch (Exception e) {
        e.printStackTrace();
    }

}

}

注意:try-catch语句需要Java Version> = 1.7。记得从null更改字节以对应您的字节数组。