Reputation: 3025
Desclaimer My question is different from two following links
public class AppendableObjectOutputStream extends ObjectOutputStream {
public AppendableObjectOutputStream(OutputStream out) throws IOException {
super(out);
}
@Override
protected void writeStreamHeader() throws IOException {}
}
If I proceed the way like write on object to appendable stream, close stream. Then again open stream, write another object close and so on. This way I am able to read multiple objects properly.
fileOutputStream = new FileOutputStream("abc.dat",true);
outputBuffer = new BufferedOutputStream(fileOutputStream);
objectStream = new AppendableObjectOutputStream(outputBuffer);
BucketUpdate b1 = new BucketUpdate("getAllProducts1",null,"1",null);
BucketUpdate b2 = new BucketUpdate("getAllProducts2",null,"2",null);
BucketUpdate b3 = new BucketUpdate("getAllProducts3",null,"3",null);
objectStream.writeObject(b1);
objectStream.writeObject(b2);
objectStream.writeObject(b3);
objectStream.close();
Upvotes: 1
Views: 11895
Reputation: 24791
Calling ObjectOutputStream.reset() after writing each object will fix this.
Upvotes: 5
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