在 Java 中将字节 [] 写入文件 [英] Write byte[] to File in Java
问题描述
Java 中如何将字节数组转换为文件?
How to convert byte array to File in Java?
byte[] objFileBytes, File objFile
推荐答案
文件对象不包含文件的内容.它只是指向硬盘驱动器(或其他存储介质,如 SSD、USB 驱动器、网络共享)上的文件的指针.所以我认为你想要的是将它写入硬盘.
A File object doesn't contain the content of the file. It is only a pointer to the file on your hard drive (or other storage medium, like an SSD, USB drive, network share). So I think what you want is writing it to the hard drive.
您必须使用 Java API 中的某些类编写文件
You have to write the file using some classes in the Java API
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(yourFile));
bos.write(fileBytes);
bos.flush();
bos.close();
您也可以使用 Writer 而不是 OutputStream.使用 writer 将允许您编写文本 (String, char[]).
You can also use a Writer instead of an OutputStream. Using a writer will allow you to write text (String, char[]).
BufferedWriter bw = new BufferedWriter(new FileWriter(yourFile));
既然你说你想把所有东西都保存在内存中,不想写任何东西,你可以尝试使用ByteArrayInputStream
.这模拟了一个 InputStream,您可以将其传递给大多数类.
Since you said you wanted to keep everything in memory and don't want to write anything, you might try to use ByteArrayInputStream
. This simulates an InputStream, which you can pass to the most of the classes.
ByteArrayInputStream bais = new ByteArrayInputStream(yourBytes);
这篇关于在 Java 中将字节 [] 写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!