我有一个字节的数组
bytes[] doc
我怎样才能用这些字节创建一个
new File
实例?我有一个字节的数组
bytes[] doc
new File
实例? try (FileOutputStream fileOuputStream = new FileOutputStream("filename")){
fileOuputStream.write(byteArray);
}
试试这个:
Files.write(Paths.get("filename"), bytes);
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 you're done.
} catch (Exception e) {
e.printStackTrace();
}
}
}
注意:try-catch语句要求Java版本>=1.7。记得将字节从null更改为对应于您的字节数组。