The object serialization classes are stream oriented. How do I write objects to a random access file?
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.
Related Questions
- How do I handle versioning of my Externalizable classes? Do I have to write a version number to the stream?
- The object serialization classes are stream oriented. How do I write objects to a random-access file?
- Can I persist my objects using serialization instead of using a relational or object database?