To start with, you will be using the simplest write() method for a file channel that writes the data contained in a single ByteBuffer object to a file. The number of bytes written to the file is determined by the buffer's position and limit when the write() method executes. Bytes will be written starting with the byte at the buffer's current position. The number of bytes written is limit-position, which is the number returned by the remaining() method for the buffer object. The write() method returns the number of bytes that were actually written as a value of type int.
A channel write() operation can throw any of five different exceptions:
|NonWritableChannelException||Thrown if the channel was not opened for writing.|
|ClosedChannelException||Thrown if the channel is closed. Calling the close() method for the file channel will close the channel, as will calling the close() method for the file stream.|
|AsynchronousCloseException||Thrown if another thread closes the channel while the write operation is in progress.|
|ClosedByInterruptException||Thrown if another thread interrupts the current thread while the write operation is in progress.|
|IOException||Thrown if some other I/O error occurs.|
The first of these is a subclass of RuntimeException, so you do not have to catch this exception. The other four are subclasses of IOException, which must be caught, so you will normally put the write() method call in a try block. If you want to react specifically to ...