问题描述
如何转换字节数组中的Java到文件?
字节[] objFileBytes,文件OBJFILE
一个的文件对象的不包含文件的内容。它只是一个指向硬盘上你的文件。所以我想你想的是什么写入到硬盘驱动器。
您必须使用一些类的Java API中写入文件
的BufferedOutputStream BOS =新的BufferedOutputStream(新的FileOutputStream(yourFile));
bos.write(fileBytes);
bos.flush();
bos.close();
您也可以使用一个OutputStream的作家来代替。使用作家将让你写的文字(字符串和char [])。
的BufferedWriter体重=新的BufferedWriter(新的FileWriter(yourFile));
既然你说你想要把一切都在内存中,不想写什么,你可以尝试使用 ByteArrayInputStream的
。这是模拟一个InputStream,您可以传递给大部分的课程。
ByteArrayInputStream的拜斯=新ByteArrayInputStream的(yourBytes);
How to convert byte array to File in Java?
byte[] objFileBytes, File objFile
A File object doesn't contain the content of the file. It is only a pointer to the file on you hard drive. So I think what you want is writing it to the hard drive.
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();
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));
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文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!