Path & Files — The NIO.2 API
Java NIO.2 (java.nio.file, Java 7+) replaced the old java.io.File API with Path and Files — a cleaner, more expressive, and more reliable file-handling system. Path represents a file location; Files provides static utility methods for every common operation.
Path Construction & Files API
import java.nio.file.*;
import java.nio.charset.StandardCharsets;
import java.io.IOException;
import java.util.List;
public class FilesAndPath {
public static void main(String[] args) throws IOException {
// Path — represents a file/directory location (OS-agnostic)
Path home = Path.of(System.getProperty("user.home")); // Java 11+
Path catalog = home.resolve("library").resolve("catalog.txt"); // appends
Path reports = Path.of("reports", "2024", "Q1.txt"); // relative path
// Path info
System.out.println(catalog.getFileName()); // catalog.txt
System.out.println(catalog.getParent()); // /home/user/library
System.out.println(catalog.isAbsolute()); // depends on construction
System.out.println(catalog.toAbsolutePath());
// Check existence & properties
Path file = Path.of("src/data/books.txt");
System.out.println(Files.exists(file));
System.out.println(Files.isRegularFile(file));
System.out.println(Files.isDirectory(file.getParent()));
System.out.println(Files.size(file)); // bytes
// Create directories
Files.createDirectories(Path.of("output/reports/2024")); // creates all parents
// Write to file (UTF-8, overwrites)
List<String> lines = List.of("Clean Code,Robert Martin,39.99",
"Effective Java,Joshua Bloch,44.99");
Files.write(Path.of("catalog.csv"), lines, StandardCharsets.UTF_8);
// For append mode: Files.write(path, lines, UTF_8, APPEND) — StandardOpenOption.APPEND
// Read all lines at once (good for small files)
List<String> readLines = Files.readAllLines(Path.of("catalog.csv"), StandardCharsets.UTF_8);
readLines.forEach(System.out::println);
// Read entire file as a string
String content = Files.readString(Path.of("catalog.csv")); // Java 11+
// Copy & move
Files.copy(Path.of("source.txt"), Path.of("backup.txt"),
StandardCopyOption.REPLACE_EXISTING);
Files.move(Path.of("temp.txt"), Path.of("archive/temp.txt"),
StandardCopyOption.REPLACE_EXISTING);
// Delete
Files.deleteIfExists(Path.of("temp.txt")); // no exception if not found
// Walk directory tree
try (var stream = Files.walk(Path.of("src"))) {
stream.filter(Files::isRegularFile)
.filter(p -> p.toString().endsWith(".java"))
.forEach(System.out::println);
}
// List directory (one level)
try (var entries = Files.list(Path.of("."))) {
entries.sorted().forEach(System.out::println);
}
}
}Quick Quiz
Tip
Tip
Practice Path Files The NIO2 API in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
JVM enables 'Write Once, Run Anywhere' — bytecode runs on any platform
Practice Task
Note
Practice Task — (1) Write a working example of Path Files The NIO2 API from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Path Files The NIO2 API is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.