☕ Java Advanced

What is Java NIO and how does it differ from traditional IO?

Answer

Java NIO (New I/O), introduced in Java 1.4, provides an alternative I/O API focused on non-blocking, buffer-oriented, and channel-based I/O. Traditional java.io is stream-oriented and blocking — each operation blocks until completed. NIO uses Channels (bidirectional connections) and Buffers (intermediate data containers) instead of streams. The key feature is the Selector — a single thread can monitor multiple channels for readiness (data available, connection complete), enabling one thread to handle thousands of connections. This is the basis of high-performance servers. Java 7's NIO.2 (java.nio.file) added the Files and Path APIs, greatly improving file system operations.