0

谁能解释为什么作者不关闭流 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.
}

源代码

这是一个错误还是细节?

4

1 回答 1

1

这在技术上是没有必要的,因为当对象超出范围时,GC 将删除它,并且拆卸过程可能会关闭它。关闭您打开的任何 I/O 流被认为是一种很好的做法,只是为了确保这一点。

于 2012-11-06T16:47:24.943 回答