Deserializing an Object that has been serialized using ObjectOutputStream's writeObject method

77 Views Asked by At

If you serialize an Object using ObjectOutputStream's writeObject method, is the only way to deserialize it through ObjectInputStream's readObject method? Can you call ObjectInputStream's other read methods?

1

There are 1 best solutions below

0
On

Items in object streams are tagged. If you try to use the wrong readXXX() primitive to read them you will get an EOFException. The only methods you can use to read a written object are readObject() and readUnshared(). You can always read the raw bytes with the read() methods of course, but you'll probably make it impossible to use any of the readXXX() methods afterwards, by moving to an arbitrary position in the stream.