使用Java:
我有一个代表文件。byte[]
我如何将其写入文件(即。C:myfile.pdf
)
我知道它已经用输入流完成了,但我似乎无法解决它。
使用 Apache Commons IO
FileUtils.writeByteArrayToFile(new File("pathname"), myByteArray)
或者,如果你坚持为自己工作……
try (FileOutputStream fos = new FileOutputStream("pathname")) {
fos.write(myByteArray);
//fos.close(); There is no more need for this line since you had created the instance of "fos" inside the try. And this will automatically close the OutputStream
}
没有任何库:
try (FileOutputStream stream = new FileOutputStream(path)) {
stream.write(bytes);
}
使用谷歌番石榴:
Files.write(bytes, new File(path));
使用 Apache Commons:
FileUtils.writeByteArrayToFile(new File(path), bytes);
所有这些策略都要求您在某个时候捕获IOException。
另一种解决方案使用:java.nio.file
byte[] bytes = ...;
Path path = Paths.get("C:\myfile.pdf");
Files.write(path, bytes);
模板简介:该模板名称为【如何把byte[] 到 Java 中的文件】,大小是暂无信息,文档格式为.编程语言,推荐使用Sublime/Dreamweaver/HBuilder打开,作品中的图片,文字等数据均可修改,图片请在作品中选中图片替换即可,文字修改直接点击文字修改即可,您也可以新增或修改作品中的内容,该模板来自用户分享,如有侵权行为请联系网站客服处理。欢迎来懒人模板【Java】栏目查找您需要的精美模板。