Java将字节[]转换为文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23533695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:40:11 来源:igfitidea点击:
Java convert bytes[] to File
提问by Bob
I have an array of bytes
我有一个字节数组
bytes[] doc
How can I create an instance of new File
from that bytes?
如何new File
从该字节创建一个实例?
采纳答案by Suren Raju
try (FileOutputStream fileOuputStream = new FileOutputStream("filename")){
fileOuputStream.write(byteArray);
}
回答by NullOverFlow
A simple program that will write a byte[] into a file.
一个将 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();
}
}
}
Note: The try-catch statement requires a Java Version >= 1.7. remember to change the bytes from null to correspond your byte array.
注意:try-catch 语句需要 Java 版本 >= 1.7。请记住将字节从 null 更改为对应于您的字节数组。
回答by Evgeniy Dorofeev
try this
尝试这个
Files.write(Paths.get("filename"), bytes);