谁能解释一下为什么作者不关闭流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.
}
这是一个错误还是具体情况?
最佳答案
这在技术上是不必要的,因为当对象超出范围时,GC 将删除该对象,并且拆卸过程可能会关闭它。关闭您打开的所有 I/O 流被认为是一个很好的做法,只是为了确保安全。
关于java - 我需要在 Android 中关闭来自 FileReader() 的流吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13253978/