The original java.io package models data as blocking streams of bytes. java.nio
adds buffers, channels, and the modern Path API, along with non-blocking modes
for scalable network servers.
The modern file API
For most file work, java.nio.file.Files is the cleanest option — no manual
stream closing, no boilerplate.
Path path = Path.of("data", "report.csv");
List<String> lines = Files.readAllLines(path);
Files.writeString(path, "done\n", StandardOpenOption.APPEND);
try (var stream = Files.lines(path)) { // lazy, for large files
long count = stream.filter(l -> l.contains("ERROR")).count();
}Buffers and channels
Channels read into reusable buffers, avoiding per-read allocation for high-throughput I/O.
try (var channel = FileChannel.open(path)) {
ByteBuffer buffer = ByteBuffer.allocate(8192);
while (channel.read(buffer) > 0) {
buffer.flip();
process(buffer);
buffer.clear();
}
}Takeaways
- Use
Fileshelpers for everyday file reads and writes. - Stream large files lazily with
Files.linesinside try-with-resources. - Reserve raw channels and buffers for performance-critical paths.
Always close Files.lines streams — they hold an open file handle until the
stream is closed.