byte []以Java格式存档 [英] byte[] to file in Java

查看:114
本文介绍了byte []以Java格式存档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Java:

我有一个 byte [] 代表一个文件。

I have a byte[] that represents a file.

如何将其写入文件(即 C:\ myfile.pdf

How do I write this to a file (ie. C:\myfile.pdf)

我知道它已经完成了InputStream,但我似乎无法解决它。

I know it's done with InputStream, but I can't seem to work it out.

推荐答案

使用 Apache Commons IO

FileUtils.writeByteArrayToFile(new File("pathname"), myByteArray)

或者,如果你坚持为自己工作......

Or, if you insist on making work for yourself...

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
}

这篇关于byte []以Java格式存档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆