NIO Package in Java
Java's NIO (New Input/Output) package, introduced in Java 1.4 and significantly enhanced in Java 7 as NIO.2, provides a modern alternative to the original ...
Non-Blocking and Modern File I/O with java.nio
Java's NIO (New Input/Output) package, introduced in Java 1.4 and significantly enhanced in Java 7 as NIO.2, provides a modern alternative to the original java.io. NIO is built around three core concepts: buffers (containers for data), channels (bidirectional data conduits), and selectors (single-thread management of multiple channels). NIO.2 added the Path, Paths, and Files utility classes that make everyday file operations far simpler than the old java.io.File approach.
Buffers and Channels In NIO, you do not read directly from a stream into your variables. Instead, a Channel connects to a data source (a file, socket, or pipe), and data moves between the channel and a Buffer object. A ByteBuffer is the most common buffer type. You put data into the buffer (write mode), flip() it to switch to read mode, then read from it. This explicit position management gives you fine-grained control but also makes the API more verbose than the straightforward java.io streams for simple use cases.
NIO.2: The Path API The java.nio.file package introduced Path as a type-safe, immutable replacement for java.io.File. A Path is obtained via Paths.get("dir/file.txt") or Path.of("dir/file.txt") in Java 11+. The Files utility class provides static methods for nearly every file operation: Files.copy(), Files.move(), Files.delete(), Files.createDirectories(), Files.readAllLines(), Files.writeString(), Files.walk() for recursive directory traversal, and Files.find() for filtered searches. These methods are cleaner, throw informative checked exceptions, and support atomic operations on some file systems.
Non-Blocking I/O and Selectors For network programming, NIO's killer feature is non-blocking I/O. A classic java.io server needs one thread per connection because read() blocks until data arrives. With NIO, a single thread can manage thousands of connections using a Selector that monitors multiple channels and delivers events only when data is ready. This event-driven model underpins high-throughput Java servers and frameworks like Netty. For file I/O, non-blocking mode is less commonly used because disk operations are handled differently by operating systems.
Code Example: import java.nio.file.*; import java.io.IOException; import java.util.List;
Path source = Path.of("source.txt"); Path dest = Path.of("backup.txt");
// Write a file Files.writeString(source, "NIO makes file ops simple.\n");
// Copy it Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING);
// Read all lines List<String> lines = Files.readAllLines(dest); lines.forEach(System.out::println);
// Walk directory tree Files.walk(Path.of("."), 2) .filter(Files::isRegularFile) .forEach(System.out::println);
Interview Tip: The question "when would you use NIO instead of java.io?" has a structured answer: use NIO channels and selectors for high-throughput non-blocking network servers managing thousands of simultaneous connections; use the NIO.2 Path/Files API for all new file system operations because it is cleaner and more capable than java.io.File; use traditional java.io streams when working with legacy code or when stream chaining via the Decorator pattern fits the design naturally. Giving this structured answer shows you understand the specific strengths of each.