NIO
NIO is the java.nio package: buffers, channels, selectors, and the java.nio.file API. It is the modern way to work with paths, files, and non-blocking sockets.
Detailed Explanation
java.io is stream-shaped: you read the next byte. NIO (New I/O, Java 1.4, then NIO.2 in Java 7) is buffer- and channel-shaped. A ByteBuffer is a fixed block of memory with a position, limit, and capacity. A Channel reads into a buffer or writes from one. You flip the buffer to switch from writing to reading. That model is what non-blocking servers and FileChannel.transferTo use.
Most application code never flips a buffer by hand. The useful everyday surface is java.nio.file. Path is a path; it is not an open file. Paths.get and Path.of build one. Files holds the operations: readString, writeString, copy, move, delete, walk, createDirectories, size, exists. They take a Path and a Charset or OpenOptions (CREATE, TRUNCATE_EXISTING, APPEND).
Files.walk and Files.find return streams of paths. Those streams wrap a directory handle. You must close the stream (try-with-resources) or you leak. Files.lines is the same: a stream over a file that must be closed.
NIO also includes Charset encoders, FileLock, memory-mapped files (FileChannel.map), and selectors for multiplexing many non-blocking sockets. Frameworks (Netty, Tomcat, the JDK HTTP server) sit on that layer. You reach for selectors only when you are writing that kind of server. You reach for Files whenever you have a filesystem path.
Compared with File, Path handles non-default filesystems (jar:, custom providers) and uses Files for operations instead of methods on the path object. File still appears in old APIs; convert with file.toPath() and path.toFile().
The cost of NIO buffers is a learning curve (flip, compact, clear are easy to swap). The cost of Files.walk without closing is a leaked iterator. The cost of Files.readAllBytes on a huge file is a huge heap allocation; stream or channel instead. Prefer Path/Files for new code, name the charset, and close walk streams.
Code Examples
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
public class WriteRead {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("nio");
Path file = dir.resolve("note.txt");
Files.writeString(file, "hello", StandardCharsets.UTF_8);
String text = Files.readString(file, StandardCharsets.UTF_8);
System.out.println(text); // prints: hello
Files.delete(file);
Files.delete(dir);
}
}import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;
public class Walk {
static long countFiles(Path root) throws IOException {
try (Stream<Path> paths = Files.walk(root)) { // must close
return paths.filter(Files::isRegularFile).count();
}
}
}Common Gotchas
`Files.walk` / `Files.lines` leak if you skip try-with-resources
the directory or file handle stays open.
`Path` is not resolved against the real file until you call `Files`
a path can point at nothing; `exists` is a separate check and can race.
`ByteBuffer.flip` sets limit to position and position to 0
calling `flip` twice shrinks the limit to 0 and looks like empty data.
`Files.readAllBytes` on a multi-gigabyte file can throw `OutOfMemoryError`
stream the file.