Reputation: 29
I'm having this problem when trying to write into a txt file on my server from a Java program. Even though it writes the text, it writes some strange chars in front of it. My code looks like this:
URL urlOutput = new URL("ftp://username:[email protected]");
URLConnection urlc = urlOutput.openConnection();
OutputStream os = urlc.getOutputStream();
OutputStream buffer = new BufferedOutputStream(os);
ObjectOutput output = new ObjectOutputStream(buffer);
output.writeObject("Hello world!");
output.close();
buffer.close();
os.close();
And this is what appears in the txt file:
¨ŪtKVHello world!
Thanks for help!
Upvotes: 1
Views: 30
Reputation: 726849
ObjectOutputStream
is used for object serialization. The part preceding "Hello world!"
is the "bookkeeping" information saved by the object output stream for the object input stream to figure out what kind of object is being restored.
Use PrintStream
for outputting textual information:
URL urlOutput = new URL("ftp://username:[email protected]");
URLConnection urlc = urlOutput.openConnection();
OutputStream os = urlc.getOutputStream();
OutputStream buffer = new BufferedOutputStream(os);
PrintStream output = new PrintStream(buffer);
output.writeLine("Hello world!");
output.close();
buffer.close();
os.close();
Upvotes: 1