2012-11-06 42 views
0

任何人都可以解释为什么作者不关闭流LineNumberReader lnr?这不是必要的吗?我是否需要关闭Android中FileReader()的流?

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

这是一个错误或具体情况如何?

回答

1

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

相关问题