ObjectOutputStream
s are used with serialization, and the first thing an ObjectOutputStream
writes is the serialization
stream header. This header should appear once per file, at the beginning. Pass a file opened in append mode into an ObjectOutputStream
constructor and the serialization stream header will be added to the end of the file before your object is then also appended.
When you’re trying to read your object(s) back from the file, only the first one will be read successfully, and a
StreamCorruptedException
will be thrown after that.
Noncompliant Code Example
FileOutputStream fos = new FileOutputStream (fileName , true); // fos opened in append mode
ObjectOutputStream out = new ObjectOutputStream(fos); // Noncompliant
Compliant Solution
FileOutputStream fos = new FileOutputStream (fileName);
ObjectOutputStream out = new ObjectOutputStream(fos);