GCGrowCarrer

File I/O (NIO)

Locked

Lesson 23 of 60 · Syntax · Medium

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

Your challenge

2 sample tests, 1 hidden

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

Opens up next

This is the last lesson in this branch.