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?
-
3Lots of comments sound like "why you won't close the stream" but I think your question was not about "why" but rather "why explicity". – Ustaman Sangat Mar 01 '12 at 20:44
6 Answers
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.

- 305,152
- 44
- 369
- 561
-
-
I'm definitely going to recycle the _habit_ wisdom. That's a quotable quote right there. – markdsievers Jun 07 '17 at 23:20
-
That quotable quote ("First we make our habits, then they make us.") is from Stephen Covey. – Dan R. Apr 28 '20 at 14:29
-
11 years late and wrong. Attributed to John Dryden, a poet who died in 1700. Covey isn't that smart. https://quoteinvestigator.com/2016/12/19/habits-make/ – duffymo Apr 28 '20 at 18:01
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.

- 1,421,763
- 867
- 9,128
- 9,194
-
7And what about when the stream object is going out of scope (and garbage-collected)? Will it be closed? – Zippo Jan 15 '11 at 10:14
-
5@Zippo: Just because a variable goes out of scope doesn't mean the object will be garbage collected. Garbage collection is non-deterministic. Some streams may have a finalizer which will flush them - but it would be a very bad idea to rely on that, IMO. – Jon Skeet Jan 15 '11 at 10:18
-
So when using `Thread.stop()` in Java is easy but almost useless when the thread uses streams... Thanks for info. – Zippo Jan 15 '11 at 10:57
-
1@Zippo: Thread.stop() is deprecated anyway, as it's generally a bad idea to stop a thread rather than ask it to stop itself in a controlled way. – Jon Skeet Jan 15 '11 at 11:33
-
1Java 8 will have some features where you can have streams automatically closed upon exiting a scope, comparable to how monitor locks are realease upon exiting a synchronized block. – Ustaman Sangat Mar 01 '12 at 20:42
-
1@UstamanSangat: It's in Java 7, actually: http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html (Assuming that's the feature you mean...) – Jon Skeet Mar 01 '12 at 20:43
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.

- 13,631
- 10
- 59
- 101
-
6Just curious, why is this voted down? Seems like a good answer to me although funny :) – Srikanth Feb 05 '09 at 14:45
-
3Naturally, I'd to know, too. :) But maybe the commenter just didn't appreciate that the answer is informative AND humorous. – Don Branson Feb 05 '09 at 14:52
-
3
-
10Personally, because the answer has virtually no technical content -- it would have been a better comment. It doesn't have much more to say than "it's a good practice", and while we often accept such explanations for expediency in our projects, it's not a good enough answer for Stack Overflow. No personal offence meant to you, Don; I'm sure you know what you're talking about, but I prefer answers with more meat. – DavidS Aug 27 '15 at 17:09
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.
-
1And Java 8 is supposed reduce the boiler plate involved with finally. – Ustaman Sangat Mar 01 '12 at 20:45
-
5
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 :)

- 7,353
- 2
- 28
- 24
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.

- 9,364
- 3
- 30
- 37