2012-11-06 50 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流是一種很好的做法,只是爲了確保。

相關問題