Java 如果从try with resource返回InputStream是安全的
从try with resource语句返回输入流以在调用方使用该流后处理该流的关闭是否安全Java 如果从try with resource返回InputStream是安全的,java,return,inputstream,try-with-resources,Java,Return,Inputstream,Try With Resources,从try with resource语句返回输入流以在调用方使用该流后处理该流的关闭是否安全 public static InputStream example() throws IOException { ... try (InputStream is = ...) { return is; } } 它是安全的,但它将被关闭,所以我认为它不是特别有用。。。无法重新打开已关闭的流 请参见此示例: public static void main(Strin
public static InputStream example() throws IOException {
...
try (InputStream is = ...) {
return is;
}
}
它是安全的,但它将被关闭,所以我认为它不是特别有用。。。无法重新打开已关闭的流 请参见此示例:
public static void main(String[] argv) throws Exception {
System.out.println(example());
}
public static InputStream example() throws IOException {
try (InputStream is = Files.newInputStream(Paths.get("test.txt"))) {
System.out.println(is);
return is;
}
}
输出:
sun.nio.ch.ChannelInputStream@1db9742
sun.nio.ch.ChannelInputStream@1db9742
sun.nio.ch.ChannelInputStream@1db9742 1000000
Exception in thread "main" java.nio.channels.ClosedChannelException
at sun.nio.ch.FileChannelImpl.ensureOpen(FileChannelImpl.java:109)
at sun.nio.ch.FileChannelImpl.size(FileChannelImpl.java:299)
at sun.nio.ch.ChannelInputStream.available(ChannelInputStream.java:116)
at sandbox.app.Main.main(Main.java:13)
通过引用返回相同的输入流,但它将被关闭。通过将示例修改为:
public static void main(String[] argv) throws Exception {
InputStream is = example();
System.out.println(is + " " + is.available());
}
public static InputStream example() throws IOException {
try (InputStream is = Files.newInputStream(Paths.get("test.txt"))) {
System.out.println(is + " " + is.available());
return is;
}
}
输出:
sun.nio.ch.ChannelInputStream@1db9742
sun.nio.ch.ChannelInputStream@1db9742
sun.nio.ch.ChannelInputStream@1db9742 1000000
Exception in thread "main" java.nio.channels.ClosedChannelException
at sun.nio.ch.FileChannelImpl.ensureOpen(FileChannelImpl.java:109)
at sun.nio.ch.FileChannelImpl.size(FileChannelImpl.java:299)
at sun.nio.ch.ChannelInputStream.available(ChannelInputStream.java:116)
at sandbox.app.Main.main(Main.java:13)
它是安全的,但它将被关闭,所以我认为它不是特别有用。。。无法重新打开已关闭的流 请参见此示例:
public static void main(String[] argv) throws Exception {
System.out.println(example());
}
public static InputStream example() throws IOException {
try (InputStream is = Files.newInputStream(Paths.get("test.txt"))) {
System.out.println(is);
return is;
}
}
输出:
sun.nio.ch.ChannelInputStream@1db9742
sun.nio.ch.ChannelInputStream@1db9742
sun.nio.ch.ChannelInputStream@1db9742 1000000
Exception in thread "main" java.nio.channels.ClosedChannelException
at sun.nio.ch.FileChannelImpl.ensureOpen(FileChannelImpl.java:109)
at sun.nio.ch.FileChannelImpl.size(FileChannelImpl.java:299)
at sun.nio.ch.ChannelInputStream.available(ChannelInputStream.java:116)
at sandbox.app.Main.main(Main.java:13)
通过引用返回相同的输入流,但它将被关闭。通过将示例修改为:
public static void main(String[] argv) throws Exception {
InputStream is = example();
System.out.println(is + " " + is.available());
}
public static InputStream example() throws IOException {
try (InputStream is = Files.newInputStream(Paths.get("test.txt"))) {
System.out.println(is + " " + is.available());
return is;
}
}
输出:
sun.nio.ch.ChannelInputStream@1db9742
sun.nio.ch.ChannelInputStream@1db9742
sun.nio.ch.ChannelInputStream@1db9742 1000000
Exception in thread "main" java.nio.channels.ClosedChannelException
at sun.nio.ch.FileChannelImpl.ensureOpen(FileChannelImpl.java:109)
at sun.nio.ch.FileChannelImpl.size(FileChannelImpl.java:299)
at sun.nio.ch.ChannelInputStream.available(ChannelInputStream.java:116)
at sandbox.app.Main.main(Main.java:13)
一旦退出函数执行,资源就会超出范围,因此会被垃圾收集。。。总而言之安全但无用….@CoderNeji除了通常无法判断何时将垃圾收集未引用对象的部分之外,InputStream是关闭的,而不是垃圾收集的,因为仍然存在从方法返回的“活动”引用。超出范围的是is变量,但这是另一个在编译时解决的问题。感谢您的解释一旦退出函数执行,资源就超出范围,因此垃圾被收集。。。总而言之安全但无用….@CoderNeji除了通常无法判断何时将垃圾收集未引用对象的部分之外,InputStream是关闭的,而不是垃圾收集的,因为仍然存在从方法返回的“活动”引用。超出范围的是is变量,但这是另一个在编译时解决的问题。感谢您的解释