Should I always close BufferedReader?

  • A+

Here is a line reading a file into a List:

List<String> lines =     new BufferedReader(         new InputStreamReader(classLoader.getResourceAsStream(fileName)))             .lines()             .collect(Collectors.toList()); 

Is this correct or should I assign the BufferedReader to a variable to be able to close it later?


You should always close your resources. Closing may not be a big problem for small programs which only use a couple of files quickly, since most mature OSes will close the files for you when the process completes. However, there are usually limits on how many files you can have open at one time. It is good to be tidy, so that you don't hit those limits when you start writing bigger programs. There are also other types of resources, like network and serial ports, which you may want to let others use once your program is done with them, even if it is still running.

An alternative to closing the file manually is using try-with-resources syntax, which ensures that the file will be closed properly even in case of an error:

List<String> lines; try(BufferedReader reader = new BufferedReader(         new InputStreamReader(classLoader.getResourceAsStream(fileName)))) {     lines = reader.lines().collect(Collectors.toList()); } 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: