任何人都可以解释为什么作者不关闭流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.
}
这是错误还是细节?
答案 0 :(得分:1)
技术上没有必要,因为当GC超出范围时,GC将删除该对象,并且拆除过程可能会将其关闭。关闭所有打开的I / O流被认为是一种很好的做法,只是为了确保。