java中如何关闭文件,在Java中捕获IOException后如何关闭文件?
所有,
我試圖確保在捕獲IOException時關閉我用BufferedReader打開的文件,但它看起來好像我的BufferedReader對象超出了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抱怨說它在catch塊中“找不到符號fileIn”,但是我想確保在發生IOException的情況下,Reader被關閉。在沒有圍繞第一個try
/ catch構造的丑陋的情況下,我該怎么做呢?
在這種情況下,有關最佳做法的任何提示或建議,
總結
以上是生活随笔為你收集整理的java中如何关闭文件,在Java中捕获IOException后如何关闭文件?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 牙齿怎么打桩
- 下一篇: java加按钮_如何从零开始对接第三方登