Java NIO Introduction
In this page:
What is NIO?
Java NIO (New I/O) is an alternative I/O API built around buffers, channels, and selectors, designed for higher throughput and scalability than the classic stream-based java.io API, especially when handling many connections at once.
Example: What is NIO?
public class Main {
public static void main(String[] args) {
System.out.println("NIO: buffers, channels, and selectors for higher throughput than java.io");
}
}
Login to try C/C++/Java/PHP code in the editor
Buffers
A Buffer is a fixed-size container of data with a position, limit, and capacity that track how much has been written or read, and calling flip() switches it from filling mode to draining mode so the same data can be read back out.
Example: Buffers
import java.nio.ByteBuffer;
public class Main {
public static void main(String[] args) {
ByteBuffer buffer = ByteBuffer.allocate(10);
buffer.put((byte) 65);
buffer.flip(); // switches from filling mode to draining mode
System.out.println(buffer.get());
}
}
Login to try C/C++/Java/PHP code in the editor
Channels
A Channel represents an open connection to an I/O source like a file or socket, and unlike a stream, data flows through a channel into or out of a Buffer, often moving larger blocks of data in a single operation.
Example: Channels
import java.nio.channels.*;
import java.nio.ByteBuffer;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
File file = new File("data.txt");
try (FileOutputStream fos = new FileOutputStream(file); FileChannel channel = fos.getChannel()) {
ByteBuffer buffer = ByteBuffer.wrap("hello".getBytes());
channel.write(buffer); // data flows through the channel into/out of a Buffer
}
file.delete();
System.out.println("Written through a channel");
}
}
Login to try C/C++/Java/PHP code in the editor
Selectors
A Selector allows a single thread to monitor multiple channels simultaneously for events like data being ready to read, which is how NIO enables handling thousands of connections without needing one thread per connection.
Example: Selectors
import java.nio.channels.Selector;
public class Main {
public static void main(String[] args) throws Exception {
Selector selector = Selector.open(); // one thread can monitor many channels
System.out.println("Selector open: " + selector.isOpen());
}
}
Login to try C/C++/Java/PHP code in the editor
NIO vs Classic I/O
Classic java.io streams are blocking and read data one byte or chunk at a time on a dedicated thread per connection, while NIO's buffer-and-channel model can operate non-blocking, letting far fewer threads manage far more concurrent I/O operations.
Example: NIO vs Classic I/O
public class Main {
public static void main(String[] args) {
System.out.println("Classic I/O: blocking, one thread per connection");
System.out.println("NIO: non-blocking, fewer threads manage more connections");
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: