MoveFast
MoveFast

Reputation: 3025

Appending to ObjectOutputStream (writing multiple objects w/o closing stream)

Desclaimer My question is different from two following links

Question 1

Question 2

    public class AppendableObjectOutputStream extends ObjectOutputStream {
      public AppendableObjectOutputStream(OutputStream out) throws IOException {
        super(out);
      }

      @Override
      protected void writeStreamHeader() throws IOException {}
}

Upvotes: 1

Views: 11895

Answers (2)

Suraj Chandran
Suraj Chandran

Reputation: 24791

Calling ObjectOutputStream.reset() after writing each object will fix this.

Upvotes: 5

Sergey Aslanov
Sergey Aslanov

Reputation: 2415

If you check question you mentioned, you will see that you have to use AppendableObjectOutputStream only to append objects to file, if file already contains some objects. For empty file you have to use ordinary ObjectOutputStream because the header should be written to the beginning in this case.

Upvotes: 4

Related Questions