Java保存/打开文件对象 [英] Java saving/opening File objects
问题描述
什么你的意思是文件对象;你是指 java.io.File
?
类 java.io.File
只是目录名称和文件名称的表示。它不是一个可以容纳文件内容的对象。
如果你的数据是在内存中的一个字节数组,那么是的,你可以保存到一个文件:
byte [] data = ...;
OutputStream out = new FileOutputStream(new File(C:\\someplace\\filename.dat));
out.write(data);
out.close();
请参阅基本我从甲骨文的Java教程/ O课程学习如何使用的FileInputStream
和读取和写入文件 FileOutputStream
。
After painstakingly trying to implement TCP file transfer in my conference chat application using raw byte streams, I've decided it's much easier to send files that I want transferred through object streams. The files are sent and stored at their destination (whether that is the central server or the downloading client) as in-memory File objects. However, these files are no use as just File objects - clients should be able to open them. Is there a way in java to save File objects as hard-disk files or to even open them up through Java?
What do you mean with "File objects"; do you mean java.io.File
?
Class java.io.File
is just a representation of a directory name and filename. It is not an object which can hold the contents of a file.
If you have the data in for example a byte array in memory, then yes, you can save that to a file:
byte[] data = ...;
OutputStream out = new FileOutputStream(new File("C:\\someplace\\filename.dat"));
out.write(data);
out.close();
See the Basic I/O Lesson from Oracle's Java Tutorials to learn how to read and write files with a FileInputStream
and FileOutputStream
.
这篇关于Java保存/打开文件对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!