Reputation: 25
I am using ObjectOutputStream
to write the data into a file. Following is the code snippet.
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f)))
{
oos.writeObject(allObjects);
}
Questions:
The problem being I saw once the file was corrupted and while debugging I had the above mentioned queries.
Upvotes: 2
Views: 1964
Reputation: 22017
I believe developers should rely on the published general contract.
There is no evidence that an ObjectOutputStream
's close()
method calls flush()
.
OpenJDK's ObjectOutputStream#close
is just a vendor implementation, I believe.
And it won't hurt if we flush on the try-with-resources.
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f))) {
oos.writeObject(allObjects);
oos.flush(); // What's possibly going wrong with this?
}
Upvotes: 2
Reputation: 1994
No: Closing ObjectOutputStream will automatically close FileOutputStream
No: The stream will be flushed automatically on close.
Upvotes: 6