Eyal Katz
Eyal Katz

Reputation: 41

Is calling flush onFileOutputStream enough?

So of course we must try-catch-finaly any Closable resource.

But I came across some code which sins as follows:

java.util.Properties myProps = ... reads & loads (and doesn't close Stream!) 
myProperties.store(new FileOutputStream(myFilePath), null);
System.exit(0);

java.util.Properties.store() flushes the underlying stream (the FileOutputStream) Will this be enough? Can you think of a scenario where the file won't be written? assuming that the method passes and no exception is being thrown in 'store'

Upvotes: 0

Views: 187

Answers (2)

user207421
user207421

Reputation: 310840

It is enough in this specific case, but it is nevertheless very bad practice. The FileOutputStream should be closed, not merely flushed.

Upvotes: 1

Udo Held
Udo Held

Reputation: 12538

If you don't want open file references I would close the streams. Flushing only makes sure that all changes are written to file.

Upvotes: 0

Related Questions