File I/O (NIO)
LockedLesson 23 of 60 · Syntax · Medium
Mastery0% of 85%
Up next: Variables and Primitive Types (~7 min) →Finish these first:Exceptions →Or pass the tests below to unlock it now.
1
The big idea
java.nio.file.Files and Path are the modern API. Always use try-with-resources for streams.
Think of it like this: Think of syntax and expressions like labeled measuring cups in a kitchen: each cup holds a specific kind of ingredient, and operators combine them into a new measurement.
Key words (tap to learn):
2
Read the lesson
java.nio.file.Files and Path are the modern API. Always use try-with-resources for streams.
List<String> lines = Files.readAllLines(Path.of("data.txt"));
Files.writeString(Path.of("out.txt"), "hello");
try (var s = Files.lines(path)) { s.forEach(System.out::println); }
3
Quick check
Before writing code: What should `Solution.countLines("/definitely/missing.txt")` return when your implementation of File I/O (NIO) is working?
4
2 sample tests, 1 hiddenYour challenge
Write static long countLines(String path) returning 0 if the file is missing.
Stuck? Get a hint (free)
More learning tools(compare languages, visual guide, related lessons)
Compare with another language
Similar to python.file-io (80% overlap). `with open()` ~ try-with-resources + `Files`/`Path`.
Visual walkthrough
A diagram, a step-by-step animation, and short captions for File I/O (NIO).
Mastery0%
Learn this first
- ExceptionsNot yet
Opens up next
This is the last lesson in this branch.