我发现了几个类似的问题,但我仍然无法找到问题的答案.
我知道关闭外部流就足够了,它会关闭在线创建的内部流.
BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
br.close();
Run Code Online (Sandbox Code Playgroud)
考虑下一个例子
Properties props = new Properties();
props.load(new FileInputStream(configPath));
Run Code Online (Sandbox Code Playgroud)
是应该将FileInputStream
其分配给变量然后显式关闭(或者使用try-with-resource构造),还是Java GC会在props.load()
方法调用之后立即自动关闭它,因为没有对资源的引用?