簡體   English   中英

我是否需要從Android&中的FileReader()關閉流?

[英]Do I need to close the stream from FileReader() in Android&?

誰能解釋為什么作者不關閉流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流被認為是一種好習慣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM