这是我的代码:
try {
RandomAccessFile srcFile = new RandomAccessFile("src.txt", "rw");
} catch(FileNotFoundException e) {
e.printStackTrace();
}
这段代码给了我一个警告,RandomAccessFile
对象 srcFile 永远不会关闭。
但是,如果我修改我的代码并在 try 块之外声明 srcFile(如下面的代码所示),警告就会消失。
RandomAccessFile srcFile;
try {
srcFile = new RandomAccessFile("src.txt", "rw");
} catch(FileNotFoundException e) {
e.printStackTrace();
}
为什么会发生这种情况,因为我srcFile.close();
无论如何都没有这样做?