【发布时间】:2019-10-14 05:06:09
【问题描述】:
我正在使用 OutputStream 和 InputStream 来读取和写入 .properties 文件:
try (OutputStream output = new FileOutputStream(path)) {
// ...
output.close();
} catch (IOException e) {
e.printStackTrace();
}
try (InputStream input = new FileInputStream(path)) {
// ...
input.close();
} catch (IOException e) {
e.printStackTrace();
}
但我的 IDE 说那些 close() 调用是多余的。如果我不关闭它们,我会不会有内存泄漏?这在 Java 中是如何工作的?感谢您的帮助。
【问题讨论】:
-
您正在使用
try-with-resources语句,该语句确保每个资源在语句结束时关闭。所以你不必自己做。
标签: java memory-leaks io