can anyone explain why the author does not close the stream LineNumberReader lnr? Is this not necessary?
protected static ArrayList<Mount> getMounts() throws Exception{
LineNumberReader lnr = null;
lnr = new LineNumberReader(new FileReader("/proc/mounts"));
String line;
ArrayList<Mount> mounts = new ArrayList<Mount>();
while ((line = lnr.readLine()) != null)
{
RootTools.log(line);
String[] fields = line.split(" ");
mounts.add(new Mount(new File(fields[0]), // device
new File(fields[1]), // mountPoint
fields[2], // fstype
fields[3] // flags
));
}
InternalVariables.mounts = mounts;
if (InternalVariables.mounts != null) {
return InternalVariables.mounts;
} else {
throw new Exception();
}
}
Moreover, in previous versions was:
finally {
//no need to do anything here.
}
Is this a mistake or specifics?
It’s technically not necessary since the object will be deleted by the GC when it goes out of scope, and the teardown process could close it. It’s considered a good practice to close any I/O streams you open, just to make sure.