Skip to main content
๐Ÿ“œ WAYPOINT LESSON

NIO.2: Path & Files

โญโญ intermediateโณ 13 min read๐Ÿ“ Lesson 86 of 180

Separating location from operation, naming your charset, and readAllLines vs lazy Files.lines memory discipline.

NIO.2: Path, Files, and the byte/char divide

java.nio.file separates where (Path) from what you do (Files):

Path p = Path.of("data", "2026", "sales.csv");
Files.exists(p);          // test
Files.createDirectories(p.getParent());   // mkdir -p equivalent
Files.size(p);            // bytes
Files.readAllLines(p);    // small text files (whole file in memory)
Files.lines(p);           // lazy Stream<String> โ€” use for big files
Files.writeString(p, text);
Files.readString(p);

Encoding is not optional: bytes โ†’ chars needs a charset. Files.newBufferedReader(p, StandardCharsets.UTF_8) โ€” always name it; the platform-default charset is a portability bug.

Memory discipline: readAllLines loads everything; Files.lines streams lazily and must be closed (use try-with-resources) or the file handle leaks.