java – 使用try-with-resources语句声明Stream之间的区别是什么?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 使用try-with-resources语句声明Stream之间的区别是什么?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3515字,纯文字阅读大概需要6分钟。
内容图文
![java – 使用try-with-resources语句声明Stream之间的区别是什么?](/upload/InfoBanner/zyjiaocheng/761/c20cf39d43174f3dbad285eb30f7bdca.jpg)
在Java 8中,Stream(AutoCloseable)无法重用,一旦消耗或使用它,流将被关闭.那么使用try-with-resources语句声明的实用程序是什么?
使用try-with-resources语句的示例:
public static void main(String[] args) throws IOException {
try (Stream<Path> entries
= Files.walk(Paths.get("."), 4, FileVisitOption.FOLLOW_LINKS)) {
entries.forEach(x -> System.out.println(x.toAbsolutePath()));// the entries stream will be automatically closed at this point
//..
System.out.println("Still in the Try Block");
} //The entries will be closed again because it is declared in the try-with-resources statement
}
这里是没有try catch块的相同示例
public static void main(String[] args) throws IOException {
Stream<Path> entries = Files.walk(Paths.get("."), 4, FileVisitOption.FOLLOW_LINKS);
entries.forEach(x -> System.out.println(x.toAbsolutePath()));// the entries stream will be automatically closed at this point
System.out.println("Is there a risk of resources leak ?");
}
哪一个更安全?
在一些答案后,我更新我的代码以检查流是否已关闭:
这里有新代码:
public static void main(String[] args) throws IOException {
resourceWithTry();
resourceWithoutTry();
}
private static void resourceWithTry() throws IOException {
try (Stream<Path> entries
= Files.walk(Paths.get("."), 4, FileVisitOption.FOLLOW_LINKS).onClose(() -> System.out.println("The Stream is closed"))) {
entries.forEach(x -> System.out.println(x.toAbsolutePath()));// the entries stream will be not automatically closed at this point
System.out.println("Still in the Try Block");
} //The entries will be closed again because it is declared in the try-with-resources statement
}
private static void resourceWithoutTry() throws IOException {
Stream<Path> entries
= Files.walk(Paths.get("."), 4, FileVisitOption.FOLLOW_LINKS).onClose(() -> System.out.println("Without Try: The Stream is closed"));
entries.forEach(x -> System.out.println(x.toAbsolutePath()));// the entries stream will be not automatically closed at this point
System.out.println("Still in the Try Block");
}
解决方法:
In Java 8, Stream (which is AutoCloseable) cannot be reused, once it
is consumed or used, the stream will be closed.
不完全是.
诸如forEach()之类的流终端操作不会关闭流.
它使流管道不再消耗.
哪个是不同的.
Package java.util.stream description州:
After the terminal operation is performed, the stream pipeline is
considered consumed, and can no longer be used; if you need to
traverse the same data source again, you must return to the data
source to get a new stream.
它没有说流已关闭.
因此,在以下代码中,从不调用Stream实例的AutoCloseable.close()方法:
Stream<Path> entries = Files.walk(Paths.get("."), 4, FileVisitOption.FOLLOW_LINKS);
entries.forEach(x -> System.out.println(x.toAbsolutePath()));
那么它总是需要关闭流吗? (使用显式close()调用或更好的try-with-resources)
java.util.stream.Stream< T> javadocs解释说,使用后几乎所有Stream实例都不需要使用AutoCloseable.close()关闭.
这只适用于IO流.
Streams have a close() method and implement AutoCloseable, but nearly
all stream instances do not actually need to be closed after use.
Generally, only streams whose source is an IO channel (such as those
returned by Files.lines(Path, Charset)) will require closing. Most
streams are backed by collections, arrays, or generating functions,
which require no special resource management. (If a stream does
require closing, it can be declared as a resource in a
try-with-resources statement.)
在我们的例子中,您操纵文件,因此关闭频道是有意义的.
通过使用try-with-resources,可以避免可能发生的资源耗尽异常和错误.
如果在处理过程中出现任何错误或异常,则资源可能会全部释放.
内容总结
以上是互联网集市为您收集整理的java – 使用try-with-resources语句声明Stream之间的区别是什么?全部内容,希望文章能够帮你解决java – 使用try-with-resources语句声明Stream之间的区别是什么?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。