1

I was wondering what happens to opened files/file pointers(Buffered Reader) inside of a function after the function exits?

For example

public void foo(){
    String curr;
    for (int i = 0; i < 100; i++){
        BufferedReader br = new BufferedReader(new FileReader(file_location));
        while((curr = br.readLine()) != null){
            /* do something */
        }
    }
}    

We must note that I DO NOT close the buffered reader.

Thanks.

1 Answers1

1

Basically the BufferedReader object itself is lost, there is no more way for you to access it.

But of course, the JVM might still hold some file handle for that open file. In that sense, you just create some kind of resource leak; and theoretically, when you do that very often you might run into real issues.

Further details then depend on the JVM itself; and the OS it is running on.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Thanks. That doesmake sense. But will the GC remove these file handles adfter sometime ? Also, what would happen happen if this method was a part of an object ? Would the handles be removed as soon as the object is cleaned by GC? – Stavan Karia Oct 13 '16 at 15:05
  • I could copy&paste what the "duplicated" answer is saying; but I think it is easier for both of us if you just click on that other question and read that top-bottom. If you then still have questions, feel free to contact me again. Nonetheless, if my answer was helpful enough, also consider accepting ;-) – GhostCat Oct 13 '16 at 15:08