← Back to Advanced Java Course | Chapter 13: Java NIO | Lesson 1 of 5

Java NIO Introduction

Java NIO 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.

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?

java
public class Main {
	public static void main(String[] args) {
		System.out.println("NIO: buffers, channels, and selectors for higher throughput than java.io");
	}
}

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

java
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());
	}
}

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

java
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");
	}
}

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

java
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());
	}
}

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

java
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");
	}
}
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.