Closing Streams in Java

JavaStream

Java Problem Overview


Why do we need to close a FileInputStream (and streams in general) in any case before we leave the program? What would happen otherwise? If the program stops before the input stream is closed explicitly in the program, doesn't the stream also close automatically?

Java Solutions


Solution 1 - Java

File handles are scarce, finite resources. You can run out of them if you don't clean them up properly, just like database connections.

If you've written a small program with just one user you can get away with being sloppy and not closing in a finally block.

But if you end up using that idiom in an application that has many users and file handles you might have a problem.

"First we make our habits, then they make us." I try to apply best practices even when they aren't necessary.

Solution 2 - Java

Yes, when the process terminates the unmanaged resources will be released. For InputStreams this is fine. For OutputStreams, you could lose an buffered data, so you should at least flush the stream before exiting the program.

Solution 3 - Java

Dude. If you don't close your stream, your unit test will fail. Or at least, it should. So, that's why you need to close it. ;)

And while the OS will almost certainly clean up if you just exit, they'll generally get freed up faster if you explicitly close them. Furthermore, what if your code ends up in a long-running program sometime down the road? Then they'll have problems and curse you. :(

So, it's like washing your hands after using the bathroom. Eventually someone will pay the price if you don't do it. You can get away with it for a while, but it's still a good practice.

Solution 4 - Java

In addition to Jon's answer, it is generally a good idea to close any resource.

Think of a database connection. Your database cannot have infinite connections opened, in this case when you don't need it, it's better you close it as soon as you're done with it.

It is also good to make this a habit. "finally" block is your friend. In case of C++, you can also use RAII to manage this automatically.

Solution 5 - Java

Sort of. The stream is backed by a "real" operating system file descriptor, and when the process terminates the OS will clean up any open file descriptors.

Anyway, it's good practice to always close your resources when you're done, so close the streams :)

Solution 6 - Java

If you don't close streams, you may have problems opening them back up again. This is especially true if they're hanging off the end of sockets.

Closing a stream also makes sure that data is flushed through the stream if there is any data left to send.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser42155View Question on Stackoverflow
Solution 1 - JavaduffymoView Answer on Stackoverflow
Solution 2 - JavaJon SkeetView Answer on Stackoverflow
Solution 3 - JavaDon BransonView Answer on Stackoverflow
Solution 4 - JavaSrikanthView Answer on Stackoverflow
Solution 5 - JavaJoao da SilvaView Answer on Stackoverflow
Solution 6 - JavaPhilip ReynoldsView Answer on Stackoverflow