Files, Paths, and I/O Streams
NIO.2 provides Path and Files for modern file operations.
Path path = Path.of("data", "users.txt");
Files.writeString(
path,
"Alice\nBob\n",
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING
);
String content = Files.readString(path);
Byte streams handle binary data; character readers/writers handle text. Buffered APIs reduce the cost of many small operations. For large files, stream line-by-line instead of loading the entire file.
Key Takeaways
PathandFilesare modern NIO.2 APIs.Choose byte vs character APIs based on data semantics.
Avoid loading huge files into memory when streaming is sufficient.