Question

I'm trying to understand what are the ramifications if the BufferedReader is never closed? I just realized that many of the static code has a BufferedReader and they are never closed with try...catch..finally (br.close)

most of the code looks like this:

public static boolean isProcessX24Success(String data) throws Exception
{   
    BufferedReader br = new BufferedReader(new StringReader(data));
    String line = "";
    String result = "";
    while ((line = br.readLine()) != null) 
    {
        if (line.contains("SUCCESS X24"))
            return true;   
    }   

    return false;
} 

As you can see there's no br.close in a finally. Does that present a problem? if so what is it? the call to this method (and other from such nature) is done sequentially --not by multiple threads.

Was it helpful?

Solution

No problems because you use it on a StringReader. When the scope of the BufferedReder is left it will be eligible for garbage collection. It would be a problem if you did not close e.g a FileReader because the system resource would not be closed properly and there might be a read-lock on the file until the program ends.

When writing it might be a problem, because the data might never be written to the file because of buffering.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top