Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing Streams in Java

Tags:

java

stream

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?

like image 427
user42155 Avatar asked Feb 05 '09 13:02

user42155


People also ask

Why do you need to close streams Java?

yes you need to close stream because, the stream is already full with content and when you close the stream then you can use it again. also data is flush in drive when use flush method.

How do I close an output stream?

close() method closes this output stream and releases any system resources associated with this stream. The general contract of close is that it closes the output stream. A closed stream cannot perform output operations and cannot be reopened.

Do we have to close the stream always Java?

Yes you you should close it and you can close it if you pass it into a submethod. If you pass it into a Reader though, if you call close on the Reader it will also close the stream.

What happens if you don't close a stream in Java?

If it is not cleanly closed, then those resources may remain committed to the connection for an indeterminate time; details depend on the nature and configuration of the services involved.


4 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.

like image 155
duffymo Avatar answered Oct 13 '22 13:10

duffymo


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.

like image 32
Jon Skeet Avatar answered Oct 13 '22 15:10

Jon Skeet


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.

like image 44
Don Branson Avatar answered Oct 13 '22 13:10

Don Branson


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.

like image 37
Srikanth Avatar answered Oct 13 '22 14:10

Srikanth