java - 我应该关闭使用 java.nio.file.Files.newInputStream 创建的 Streams 吗?

标签 java stream nio2

在流教程中,没有提到关闭从 Files.newInputStream(path) 获得的流。只有一些晦涩:

Whether the returned stream is asynchronously closeable and/or interruptible is highly file system provider specific and therefore not specified.

在这种情况下,什么是“异步”?如果我显式关闭流,或者另一个线程异步关闭流?

最佳答案

您绝对必须关闭获得的InputStream,就像所有其他的一样。术语“可异步关闭”指的是在另一个线程因 I/O 操作而阻塞时关闭流的能力。

来自 InterruptibleChannel文档:

A channel that implements this interface is asynchronously closeable: If a thread is blocked in an I/O operation on an interruptible channel then another thread may invoke the channel's close method. This will cause the blocked thread to receive an AsynchronousCloseException.

关于java - 我应该关闭使用 java.nio.file.Files.newInputStream 创建的 Streams 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28048325/

相关文章:

java - 如何使用 Spring MVC 对我的 API 进行版本控制?

Java I/O : Ensure a file is not locked by another process before any r/w operation

java - 为有经验的 C++ 开发人员快速切换到 Java

java - 在java代码中使用特殊字符安全吗?

c# - 在 C# 中将二进制数据写入文件的最高效方法

c# - 异步/等待竞争条件

c++ - 在 C++ 中写入二进制数据

java - NIO 的文件系统和路径与默认文件系统不一致

java - 使用 Java WatchService 监视文件夹中的文件夹

java - 如何解决错误 :Left side ($point) of '>=' operation has null value