我的问题更多是Why类型而不是How。
我知道在 Java 7 中以下工作:
try (
FileInputStream in = new FileInputStream(source);
FileOutputStream out = new FileOutputStream(target);
) {
.....................
} catch (......) {
...............
}
以下给出了语法错误:
FileInputStream in;
FileOutputStream out;
try (
in = new FileInputStream(source);
out = new FileOutputStream(target);
) {
.....................
} catch (......) {
...............
}
我很好奇为什么Closable
/Autoclosable
引用在块的本地try
如此重要?如果我们不拥有它,那么关闭它是危险的逻辑吗?