1

For a class, I have to send a file of any type from my client to a server. I have to handle each packet individually and use UDP. I have managed to transfer the file from the client to the server, and I now have a file object which I cannot figure out how to save to a user specified directory.

f = new File(path + '\\' + filename);//path and filename are user specified.
FileOutputStream foutput = new FileOutputStream(f);
ObjectOutputStream output = new ObjectOutputStream(foutput);
output.writeObject(result);//result is a File
output.flush();
output.close();

Any time I run this code, it writes a new file with the appropriate name, but the text file I am testing ends up just containing gibberish. Is there any way to convert the File object to a file in the appropriate directory?

EDIT: As it turns out, I was misunderstanding what, exactly, a file is. I have not been transferring the data, but rather the path. How do I transfer an actual file?

9
  • "now have a file object which I cannot figure out how to save to a user specified directory" .. Are you asking "How do I copy a file?" ? Commented Apr 18, 2015 at 4:14
  • 3
    I suspect you are misunderstanding what File and ObjectOutputStream do. Reread the documentation. Commented Apr 18, 2015 at 4:25
  • Using Serialization like you do is writing a binary file that represents the object - human cannot "read" it. It sounds like what you're looking for is a JSON/XML representation of your object which could be written to a file - so that a human can read it. Lookup "marshalling and unmarshalling objects in java" Commented Apr 18, 2015 at 4:29
  • Did you transfer a File object, or the bytes the file contains? Commented Apr 18, 2015 at 5:14
  • I converted the file to a series of byte arrays and sent them. I reassembled the byte arrays and converted it back to a File object using a custom class I found called ByteUtils. Commented Apr 18, 2015 at 16:17

2 Answers 2

3

ObjectOutputStream is a class that outputs a specific format of data to a text file. Only ObjectInputStream's readObject() can decoding that text file. If you open the text file , it is just gibberish ,as you have seen.

you want this:

FileOutputStream fos = new FileOutputStream(path + '\\' + filename);
FileInputStream fis = new FileInputStream(result);
byte[] buf = new byte[1024];    
int hasRead = 0;
while((hasRead = fis.read(buf)) > 0){
    fos.write(buf, 0, hasRead);
}
fis.close();
fos.close();
Sign up to request clarification or add additional context in comments.

Comments

1

If I understand your question, how about using a FileWriter?

File result = new File("result.txt");
result.createNewFile();
FileWriter writer = new FileWriter(result); 
writer.write("Hello user3821496\n"); //just an example how you can write a String to it
writer.flush();
writer.close();

2 Comments

I have to be able to transfer a file of any type. This only works with text files, correct?
That requirement was not clear from your question, but from the API: "FileWriter is meant for writing streams of characters. For writing streams of raw bytes, consider using a FileOutputStream." docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.