Java : ClosedChannelException with Examples
ClosedChannelException (Java SE 24 & JDK 24) in Java with Examples.
You will find code samples for most of the ClosedChannelException methods.
Summary
Checked exception thrown when an attempt is made to invoke or complete an I/O operation upon channel that is closed, or at least closed to that operation. That this exception is thrown does not necessarily imply that the channel is completely closed. A socket channel whose write half has been shut down, for example, may still be open for reading.
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 ()
Constructs an instance of this class.
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
Please see the link below.
Related posts
- API Examples