Saving / Opening Java File Objects

After a painstaking attempt to implement the transfer of TCP files in a conference chat application using the original byte streams, I decided it was much easier to send the files that I want to transfer through the object streams. Files are sent and saved at their destination (whether it is a central server or a client for downloading) as file objects in memory. However, these files are useless only as File objects — clients should be able to open them. Is there a way in java to save File objects as files on the hard drive or even open them through Java?

+5
source share
3 answers

What do you mean by "File Objects"; you mean java.io.File?

A class java.io.Fileis simply a representation of the directory name and file name. This is not an object that may contain the contents of a file.

If you have data, for example, in an array of bytes, then yes, you can save this in a file:

byte[] data = ...;

OutputStream out = new FileOutputStream(new File("C:\\someplace\\filename.dat"));
out.write(data);
out.close();

Check out Basic I / O Lesson from Oracle Java Tutorials to learn how to read and write files using FileInputStreamand FileOutputStream.

+8
source

You Should Learn Data Handlers

You can use them to transfer files as Data Sources, but for you in a "transparent" way.

+1
source

, . "

This is not true. Bad idea: there is memory and latency (i.e. Time and space). Just send and receive bytes before you specify the file name and file size.

0
source

All Articles