What are the bad things that can happen when I don’t close the stream?
Does the close operation automatically flush?
Are all the streams closed after the program exits?
Thanks in advance.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Bad things that can happen when you don’t close your streams:
Yes, close operation always flushes the stream.
All file handles that the OS is aware of are closed. This means effectively that
FileOutputStream,FileInputStreamand the input/output of aSocketwill be closed. But if you wrap aFileOutputStreamin aBufferedOutputStreamthen thatBufferedOutputStreamwill not be known to the OS and won’t be closed/flushed on shutdown. So data written to theBufferedOutputStreambut not yet flushed to theFileOutputStreamcan be lost.