谁能解释为什么作者不关闭流LineNumberReader 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.
}
source code
这是错误还是细节?
最佳答案
从技术上讲,这是没有必要的,因为当对象超出范围时,它将被GC删除,并且拆卸过程可能会将其关闭。为了确保安全,关闭所有打开的I / O流被认为是一种好习惯。