Thursday, March 12, 2009

The object serialization classes are stream oriented. How do I write

Question :The object serialization classes are stream oriented. How do I write
objects to a random access file? (CoreJava)

Answer :Currently there is no direct way to write objects to a random access file.
You can use the ByteArray I/O streams as an intermediate place to write
and read bytes to/from the random access file and create Object I/O
streams from the byte streams to write/read the objects. You just have to
make sure that you have the entire object in the byte stream or
reading/writing the object will fail.
For example, java.io.ByteArrayOutputStream can be used to receive the
bytes of ObjectOutputStream. From it you can get a byte[] of the result
which, in turn, can be used with ByteArrayInputStream as input to
ObjectInput.

No comments: