我看到了这段代码,想知道为什么in.close()
在finally block
. 的要点try-with resources
是它closes
是resources
正确的。
File file = new File(FILE_NAME);
FileInputStream in = null;
try (in = new FileInputStream(file)){
//do something
} catch (final FileNotFoundException e) {
log.log(Level.WARNING, "file not found, " + file.getAbsolutePath(), e);
} catch (final IOException e) {
log.log(Level.WARNING, "cannot access the file for reading", e);
} finally {
if (in != null){
try {
in.close();
} catch (final IOException e) {
log.log(Level.WARNING, "Attempt to close file failed.", e);
}
}
}
是否会出现在 Java 中使用 try-with-resources 可以打开但无法关闭文件的情况?