Java : ClosedChannelException 示例
Java 中的 ClosedChannelException (Java SE 24 & JDK 24) 及其示例。
您将找到大多数 ClosedChannelException 方法的代码示例。
注解 :
- 本文可能使用了翻译软件以方便阅读。 另请查看英文原文。
简介
当尝试在已关闭的通道上调用或完成 I/O 操作(或至少对该操作关闭)时抛出已检查异常。抛出此异常并不一定意味着通道已完全关闭。例如,一个套接字通道的写入部分已关闭,但可能仍处于读取状态。 (机器翻译)
final var path = Path.of("R:", "java-work", "test.data");
System.out.println(path); // R:\java-work\test.data
try (final var channel = FileChannel.open(path,
StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
System.out.println(channel.isOpen()); // true
channel.close();
System.out.println(channel.isOpen()); // false
try {
final var src = ByteBuffer.wrap(new byte[]{10, 20, 30});
var _ = channel.write(src);
} catch (IOException e) {
System.out.println(e);
}
// Result
// ↓
//java.nio.channels.ClosedChannelException
}
Constructors
ClosedChannelException ()
构造此类的一个实例。 (机器翻译)
final var e = new ClosedChannelException();
System.out.println(e); // java.nio.channels.ClosedChannelException
Methods declared in Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
请参阅下面的链接。
相关文章
- API 示例
- Channel
- ClosedChannelException