Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Unclosed File input stream

 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I open a file input stream as below and read the contents of a file and exit from the method without closing the input stream [fin.close()].
1) What are the implications of not closing input stream, performance related?.
2) Since fin is a local variable once the method terminates the variable will be destroyed, In this do i still need to close the input stream?

FileInputStream fin = new FileInputStream(file);
Please advice.
Thanks
Deepak
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you will look at the source code or JavaDocs for the finalize() method for classes like FileInputStream you will see that the close method will be called when the object is garbage collected. This represents the language authors last-ditch attempt to keep your program out of trouble and should never be a substitute for good practice since you have no control over when it occurs.

A poorly written program that works with a lot of files could easily run out of file "handles" causing mysterious exceptions.

There is no performance hit of any significance.

Bill
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what William hints at but does not quite say is that programmers must always close streams, files, database connections etc. that they open. This must NOT be left to the garbage collector, which has absolutely no guarantee of when it will do so.

A "finally" clause should typically be used, to ensure the stream (or whatever) is closed, even when there is an exception.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason for the need to close a stream is that each stream you open will take up resources from the operating system (for example file handles) - and those resources are freed when you close the stream. On most (all?) systems, the number of available resources is limited.

So what could happen is that when you open a lot of streams without ever closing them, at some time you won't be able to open another stream because there are no resources left (trying to do so will raise an Exception/Error).
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Additionally, if you don't close streams ourself, you my be unable to perform other operations involving the same file. For example, if you try to rename a file, or delete it, or open a new stream that writes to the file, those operations may fail because the original stream is still open.

The finalize() method can make your results unpredictable, too, because while it will usually cause a forgotten stream to close eventually, it may not close the stream soon enough. And the results will vary depending on many factors, including the amount of load the machine is under. You can get a program that functions perfectly well most of the time, but fails mysteriously under stress, because garbage collection is not fast enough to close your streams in time. Thus, it's much, much better to close your streams yourself.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For OutputStreams, it gets even worse - when you don't properly close the stream, it could even happen that (for example) the file isn't completely written to disk. Note that there is no guarantee that the finalize method is called when the JVM terminates.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!