java中如何关闭文件,在Java中捕获IOException后如何关闭文件?
所有,
我試圖確保在捕獲IOException時(shí)關(guān)閉我用BufferedReader打開(kāi)的文件,但它看起來(lái)好像我的BufferedReader對(duì)象超出了catch塊的范圍。
public static ArrayList readFiletoArrayList(String fileName, ArrayList fileArrayList)
{
fileArrayList.removeAll(fileArrayList);
try {
//open the file for reading
BufferedReader fileIn = new BufferedReader(new FileReader(fileName));
// add line by line to array list, until end of file is reached
// when buffered reader returns null (todo).
while(true){
fileArrayList.add(fileIn.readLine());
}
}catch(IOException e){
fileArrayList.removeAll(fileArrayList);
fileIn.close();
return fileArrayList; //returned empty. Dealt with in calling code.
}
}
Netbeans抱怨說(shuō)它在catch塊中“找不到符號(hào)fileIn”,但是我想確保在發(fā)生IOException的情況下,Reader被關(guān)閉。在沒(méi)有圍繞第一個(gè)try
/ catch構(gòu)造的丑陋的情況下,我該怎么做呢?
在這種情況下,有關(guān)最佳做法的任何提示或建議,
總結(jié)
以上是生活随笔為你收集整理的java中如何关闭文件,在Java中捕获IOException后如何关闭文件?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 牙齿怎么打桩
- 下一篇: java加按钮_如何从零开始对接第三方登