java - Should I also flush() a stream when I'm done with it, or is close() enough? -


after i'm done using bufferedwriter, know should call writer.close(). should call writer.flush()? or unnecessary?

also, matter if inside try block containing instantiation of stream , io code - or outside of it?

close automatically flush stream, yoou dont have flush stream while closing. flush useful when in middle of writing stream , want push intermediate values file/output stream, if program crashes data wont lost.

from java 7 , higher can make use of try-with-resources avoid writing clean code cleaning streams.

the try-with-resources statement try statement declares 1 or more resources. resource object must closed after program finished it. try-with-resources statement ensures each resource closed @ end of statement.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -