"Frank B. Brokken" wrote:
>
> Dear listmembers,
>
> I have a problem using FileOutputStreams. I want to be able to append to
> an existing file, and I would like to be able to read the information back
> later.
>
If you want to write the Strings as text appended to the file, you're
better off using a RandomAccessFile and calling writeUTF(String).
try {
RandomAccessFile raf = new RandomAccessFile("myfile", "rw");
raf.seek(raf.length());
raf.writeUTF("hello\n");
raf.writeUTF("world\n"); // omit '\n' if you want the words
// on one line
raf.close();
} catch (IOException ioe) {
// do something with it
}
Reading in a String is done with readUTF().
If you need this to go into a binary format file, writeUTF() and
readUTF() are useful as well. The writeObject() methods don't work well
for appending files, as they are object serialization methods and expect
a single object to be read/written per file. Appending objects onto one
another is likely causing the class verifier to think the objects are
corrupted.
--
Jeff Galyan
http://www.anamorphic.com
http://www.sun.com
jeffrey dot galyan at sun dot com
talisman at anamorphic dot com
Sun Certified Java(TM) Programmer
======================================================================
Linus Torvalds on Microsoft and software development:
"... if it's a hobby for me and a job for you, why are you doing such a
shoddy job of it?"
The views expressed herein do not necessarily reflect those of my
employer.
Sun Microsystems, Inc., has no connection to my involvement with the
Mozilla Organization.
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]