Java's java.nio
package provides APIs for performing non-blocking input/output (I/O) operations in Java. Non-blocking I/O means that a thread can initiate an I/O operation without having to wait for the operation to complete. This can lead to more efficient I/O operations, as a thread can perform other work while the I/O operation is being performed.
The java.nio
package was introduced in Java 1.4 and provides the following:
Buffer
classes for storing data in various primitive typesChannel
classes for performing I/O operationsSelector
classes for multiplexing I/O operationsFileLock
and FileChannel
classes for performing file-based I/O operationsIn addition, the java.nio.file
package was introduced in Java 7 and provides support for file-based I/O operations.
A Buffer
is a container for data of a specific primitive type. There are Buffer
classes for each of the primitive data types: ByteBuffer
, CharBuffer
, DoubleBuffer
, FloatBuffer
, IntBuffer
, LongBuffer
, and ShortBuffer
.
A Buffer
has the following properties:
A Buffer
also has the following methods:
clear()
: Sets the position to 0 and the limit to the capacity.flip()
: Sets the limit to the current position and the position to 0.rewind()
: Sets the position to 0.remaining()
: Returns the number of elements between the position and the limit.hasRemaining()
: Returns true
if there are elements remaining, false
otherwise.A Channel
is a Buffer
-like structure that represents an open connection to an I/O device. A Channel
can be open for reading, writing, or both.
There are several types of Channel
s, including FileChannel
, DatagramChannel
, SocketChannel
, and Pipe.SinkChannel
.
A Channel
has the following methods:
isOpen()
: Returns true
if the channel is open, false
otherwise.close()
: Closes the channel.read(Buffer)
: Reads data from the channel into the given buffer.write(Buffer)
: Writes data from the given buffer into the channel.A Selector
is used for multiplexing I/O operations. A Selector
can be used to wait for I/O operations to complete on one or more Channel
s.
A Selector
has the following methods:
open()
: Opens a Selector
.close()
: Closes the Selector
.select()
: Returns the SelectionKey
s for the Channel
s on which I/O operations are ready to be performed.select(long)
: Returns the SelectionKey
s for the Channel
s on which I/O operations are ready to be performed, or null
if the timeout expires.selectedKeys()
: Returns the Set
of SelectionKey
s for the Channel
s on which I/O operations are ready to be performed.keys()
: Returns the Set
of SelectionKey
s for all Channel
s registered with this Selector
.A FileLock
is used to lock a region of a FileChannel
. A FileLock
has the following methods:
isShared()
: Returns true
if the lock is shared, false
otherwise.release()
: Releases the lock.isValid()
: Returns true
if the lock is still valid, false
otherwise.A FileChannel
is a Channel
that is used for performing file-based I/O operations. A FileChannel
has the following methods:
lock(long, long, boolean)
: Locks a region of the file.tryLock()
: Tries to lock a region of the file.read(ByteBuffer, long)
: Reads data from the file into the given buffer.write(ByteBuffer, long)
: Writes data from the given buffer into the file.The java.nio.file
package was introduced in Java 7 and provides support for file-based I/O operations. This package contains the following classes:
Path
: A representation of a file system path.Paths
: A factory for creating Path
objects.Files
: A utility class for performing file-related operations.Java's java.nio
package provides APIs for performing non-blocking I/O operations in Java. The java.nio.file
package was introduced in Java 7 and provides support for file-based I/O operations.