tags:

views:

80

answers:

3

i noticed in a java program the below line used to open a file and process it

BufferedReader inp = new BufferedReader(new FileReader(inputFile));

In the javaprogram the inp is not closed before exiting the program the below line is missing

if (inp != null)
    try {
       inp.close();
    } catch (IOException logOrIgnore) {}

The program has exits in a lot of place but they had not closed the file. Do i need to put this line everywhere? If i dont close the file when the program exits will it be a issue. Does the garbage collector closes the file?

+3  A: 

You should use try/finally:

Reader inp = new BufferedReader(new FileReader(inputFile));
try {
    // Do stuff with "inp"
} finally {
    IOUtils.closeQuietly(inp);
}

IOUtils is from Apache Commons IO. Its closeQuietly method is like your code snippet above: it calls close, and ignores any exceptions thrown.

Chris Jester-Young
Thanks a lot for the info.
Arav
A: 

The garbage collector does not close the file. If you know your program will not be long running or open many files, you can get away without closing the file. But otherwise you need to close it manually.

Matt McHenry
function() {BufferedReader inp = new BufferedReader(new FileReader(inputFile)); }Since it's defined inside a object function will it not get destroyed automatcally after function exit? Will it make it a issue if i dont close it?
Arav
The garbage collector *does* close the file, but there is no guarantee it will ever execute.The OS will also close the file when the process exits. For input files this is good enough. For output files with buffers in the application space such as BufferedOutputStream, BufferedWriter, ObjectOutputStream, PrintStream, PrintWriter, it isn't.
EJP
@EJP: There is nothing in the javadoc for FileReader or BufferedReader that says it will be closed automatically. They both have an explict close() method. They both inherit finalize() from Object, so I don't see how anything reader-specific could happen when the object is GC'd. (You're of course correct that the OS will close the file when the process exits.)
Matt McHenry
A: 

It sounds like you're using the BufferedReader without returning to the context in which it was declared (possibly an instance variable?). In that instance, you must close it manually upon each possible exit from your application. You cannot rely on the garbage collector to do this for you.

Joel
function() { BufferedReader inp = new BufferedReader(new FileReader(inputFile)); } Since it's defined inside a object function will it not get destroyed automatcally after function exit? Will it make it a issue if i dont close it?
Arav
@arav: It will get garbage collected eventually (probably soon, but no guarantees of promptness), but, that won't happen straight away at function exit. The try/finally approach allows the closing of the file to happen exactly when you expect it to happen ("deterministic disposal").
Chris Jester-Young
@arav: As to whether it's an issue if you don't close it explicitly, if you call that function too many times before the garbage collector can finalize your readers, then you will run out of available file descriptors, and no further opens will succeed (until some existing file descriptors get closed).
Chris Jester-Young