My question is whether it is strictly mandatory to close a BufferedReader
with .close()
.
I'm practicing with reading and writing files and, for example, if BufferedWriter
you don't close it with .close()
it doesn't do anything, therefore it is mandatory to close it.
Does the same thing happen with BufferedReader
, that is, is it strictly mandatory to close it? Or can it be left unlocked? Is there any drawback?
It is not strictly mandatory to close BufferedReaders, but it is good practice to do so, since by closing a BufferedReader you are freeing resources that you no longer need. What happens if you leave BufferedReader without closing because your application may have memory and performance problems.
So my advice to you is to close all the BufferedReader and BufferedWriter that you no longer need.
I don't know what version of java you are using, but as of Java 7 you can do that:
This is called try-with-resources , whatever is declared inside the statement
try
will be closed automatically when the block of statements it is intry
are fully executed.