The goal of this tutorial is to provide an understanding of Java's New Input/Output (NIO) APIs, which enable high-speed, scalable file operations. By the end of the tutorial, you will have learned:
Prerequisites for this tutorial are a basic understanding of Java and its standard I/O operations.
Java NIO provides many features that standard I/O does not, like non-blocking I/O operations, buffer for data manipulation, and channels to provide a connection for I/O operations.
In standard I/O, you work with streams, but in NIO you work with channels and buffers. Data is always read from a channel into a buffer, or written from a buffer into a channel.
try (FileChannel channel = FileChannel.open(Paths.get("file.txt"), StandardOpenOption.READ)) {
ByteBuffer buffer = ByteBuffer.allocate(1024);
int bytesRead = channel.read(buffer);
}
In the above code, we first open a FileChannel
to the file.txt
file. Then we create a ByteBuffer
with a capacity of 1024 bytes. The channel.read(buffer)
call reads data from the channel into the buffer.
Java NIO allows you to do non-blocking I/O operations. This means that you can do other things while waiting for I/O operations to complete.
try (ServerSocketChannel serverChannel = ServerSocketChannel.open()) {
serverChannel.configureBlocking(false);
// other operations
}
In the above example, serverChannel.configureBlocking(false)
puts the serverChannel
into non-blocking mode.
Let's see a couple of examples of how to use Java NIO for file operations.
Path path = Paths.get("file.txt");
try (SeekableByteChannel sbc = Files.newByteChannel(path, StandardOpenOption.READ)) {
ByteBuffer buf = ByteBuffer.allocate(1024);
while (sbc.read(buf) > 0) {
buf.rewind();
System.out.print(Charset.defaultCharset().decode(buf));
buf.flip();
}
}
In this example, we first create a Path
to the file.txt
file. Then we open a new SeekableByteChannel
to the file. We read data from the channel into the buffer until there's no more data to read. When the buffer is full, we rewind it, decode the data into characters, and print them.
Path path = Paths.get("file.txt");
try (FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.WRITE)) {
String data = "Hello, NIO!";
ByteBuffer buffer = ByteBuffer.wrap(data.getBytes());
fileChannel.write(buffer);
}
In this example, we first create a Path
to the file.txt
file. Then we open a new FileChannel
to the file. We wrap the data into a ByteBuffer
and write it to the file.
In this tutorial, we have learned about Java NIO, including channels and buffers, and how to perform non-blocking I/O operations. We have also looked at examples of how to read from and write to a file using Java NIO.
For further learning, you may want to explore the Selector in Java NIO, which allows a single thread to manage multiple channels.
You may use the examples in this tutorial as a starting point. Remember, practice is key to mastering any concept.
Path sourcePath = Paths.get("source.txt");
Path destinationPath = Paths.get("destination.txt");
try (FileChannel sourceChannel = FileChannel.open(sourcePath, StandardOpenOption.READ);
FileChannel destinationChannel = FileChannel.open(destinationPath, StandardOpenOption.WRITE)) {
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (sourceChannel.read(buffer) > 0) {
buffer.flip();
while (buffer.hasRemaining()) {
destinationChannel.write(buffer);
}
buffer.clear();
}
}
Path sourcePath = Paths.get("largefile.txt");
Path destinationPath = Paths.get("output.txt");
try (FileChannel sourceChannel = FileChannel.open(sourcePath, StandardOpenOption.READ);
FileChannel destinationChannel = FileChannel.open(destinationPath, StandardOpenOption.WRITE)) {
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (sourceChannel.read(buffer) > 0) {
buffer.flip();
while (buffer.hasRemaining()) {
destinationChannel.write(buffer);
}
buffer.clear();
}
}
In both solutions, we first create Path
to the source and destination files. Then we open a FileChannel
to each file. We read data from the source file into a buffer, and then write from the buffer to the destination file. We do this until there's no more data to read from the source file.