Lesson content
Read, practise, then check your understanding
Byte streams use InputStream/OutputStream; character I/O uses Reader/Writer with a charset. NIO Path and Files provide concise filesystem operations.
Explicit encoding and cleanup
Path path = Path.of("notes.txt");
try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
writer.write("Java\n");
}
try (Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8)) {
lines.forEach(System.out::println);
}
Buffer repeated small operations, check size before loading untrusted files, normalize and constrain user-controlled paths, and handle partial failure. Files.move can support temporary-file replacement. Never rely on the platform default charset for durable interchange. Streams from Files.lines hold resources and must be closed.
Knowledge check
Answer every question correctly to complete this chapter.
0 of 10 checks passed
Your progress is saved on this device.