Java NIO (New Input/Output) is an advanced I/O API introduced in Java 1.4 that provides high-performance, non-blocking I/O operations. It is designed for scalable applications such as servers, file systems, and network programs.
Java NIO works mainly with three core components:
// Java NIO example using ByteBuffer
import java.nio.ByteBuffer;
public class NioIntro {
public static void main(String[] args) {
ByteBuffer buffer = ByteBuffer.allocate(10);
buffer.put((byte) 65);
buffer.flip();
while (buffer.hasRemaining()) {
System.out.println((char) buffer.get());
}
}
}
The program prints:
A
The buffer stores byte data, switches to read mode using flip(), and then reads the value.
Start by putting data into the buffer.
clear() and rewind()