任何人都可以解释为什么作者不关闭流式亚元素lnr?这不是必需的吗?
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();
}
}
此外,在以前的版本中是:
finally {
//no need to do anything here.
}
源代码
这是错误还是具体说明?
从技术上讲,这是不必要的,因为当对象脱离范围时,该对象将被GC删除,并且拆卸过程可能会关闭。为了确保,关闭打开的任何I/O流是一种好习惯。