[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: File IO troubles...
ObjectOutputStream writes the serialized representation of a Java object or
primitive. The extra bytes you're getting in your output file are
headers/footers that provide the information needed to unserialize the data
back to a Java object.
Try PrintStream instead. Also, you may find the I/O section of Sun's Java
tutorial (http://java.sun.com/docs/books/tutorial/essential/io/index.html)
to be helpful; it's a very concise introduction.
-------------------------------------------------
Brian Sanders -- Java Developer -- Risk Labs, LLC
678-784-4614 voice, 678-784-4714 fax
-----Original Message-----
Subject: File IO troubles...
I'm having trouble writing a text file. My makeFile method is listed below.
The problem is that I'm getting some strange characters and some null bytes
at the beginning of the file as well as about every 1k characters. I don't
have this problem when I write to the console. Any clues why my file io is
adding more stuff? Even by creating the File object and not writing
anything to it I get 4 null bytes at the beginning of the file.
Thanks!
Deron Pardue
****************************BEGIN CODE********************************
public void makeFile(StringBuffer sb, String title)
{
ObjectOutputStream output;
File filename = new File("c:\\" + title);
try
{
output = new ObjectOutputStream(new FileOutputStream(filename));
String s = sb.toString();
byte ab[] = new byte[s.length()];
ab = s.getBytes();
output.write(ab);
output.close();
}
catch (IOException e)
{
System.out.println("Error writing file " + title + ":\n" + e);
System.exit(0);
}
}
****************************END CODE********************************