I am able to write an object using ObjectOutputStream but when I try to read the object back using ObjectInputStream I get a StreamCorruptedException. How can I overcome this?

Tim Rohaly

The serialization specification says that a StreamCorruptedException is thrown:

  • If the stream header is invalid.
  • If control information not found.
  • If control information is invalid.
  • JDK 1.1.5 or less attempts to call readExternal() on a PROTOCOL_VERSION_2 stream.
The last item is probably the most common problem - it will occur if you write out an Externalizable object using Java version 1.2 and try to read it in using Java version 1.1.5 or less. The other reasons might occur if, for example, you transferred the .ser file to a different computer by FTP and forgot to use binary mode.

One additional cause of StreamCorruptedException that is not explicitly mentioned in the specification is if you try to deserialize a String of length >64kB into a pre-version 1.3 JVM. Prior to Java version 1.3, serialization of String objects of this length was not supported.