← Back to Advanced Java Course | Chapter 13: Java NIO | Lesson 2 of 5

Java Path & Files API

The NIO.2 Path interface represents file locations in a platform-independent way, and the Files utility class provides convenient methods for reading, writing, and walking file trees.

The Path Interface

The Path interface, part of the NIO.2 API introduced in Java 7, represents a file or directory location in a platform-independent way, and Path.of is the modern factory method used to construct one from a series of path segments.

Example: The Path Interface

java
import java.nio.file.Path;
public class Main {
	public static void main(String[] args) {
		Path path = Path.of("data", "report.txt"); // platform-independent, built from segments
		System.out.println(path);
	}
}

Creating and Resolving Paths

Paths can be built up and inspected without ever touching the file system: resolve joins a base path with a relative one, while getParent and getFileName extract individual pieces of an existing path.

Example: Creating and Resolving Paths

java
import java.nio.file.Path;
public class Main {
	public static void main(String[] args) {
		Path base = Path.of("data");
		Path full = base.resolve("report.txt"); // joins base with a relative path
		System.out.println(full.getParent() + " " + full.getFileName());
	}
}

Reading Files with Files

The Files utility class provides convenient static methods like readString and readAllLines that read a file's entire content into a String or a List of lines in a single call, without manually managing streams or readers.

Example: Reading Files with Files

java
import java.nio.file.*;
public class Main {
	public static void main(String[] args) throws Exception {
		Path file = Path.of("sample.txt");
		Files.writeString(file, "hello world");
		String content = Files.readString(file); // whole file in one call
		System.out.println(content);
		Files.delete(file);
	}
}

Writing Files with Files

Files also provides writeString and other write methods for creating or updating file content in one call, and options like StandardOpenOption.APPEND control whether new content overwrites or is added to the end of an existing file.

Example: Writing Files with Files

java
import java.nio.file.*;
public class Main {
	public static void main(String[] args) throws Exception {
		Path file = Path.of("log.txt");
		Files.writeString(file, "first line\n");
		Files.writeString(file, "second line\n", StandardOpenOption.APPEND); // added, not overwritten
		System.out.println(Files.readString(file));
		Files.delete(file);
	}
}

Walking a Directory Tree

Files.list and Files.walk return a lazy Stream of Path objects representing a directory's contents, with walk recursing into subdirectories, making it easy to filter, count, or process an entire file tree using standard Stream operations.

Example: Walking a Directory Tree

java
import java.nio.file.*;
import java.util.stream.*;
public class Main {
	public static void main(String[] args) throws Exception {
		Path dir = Files.createTempDirectory("demo");
		Files.createFile(dir.resolve("a.txt"));
		try (Stream<Path> paths = Files.walk(dir)) { // recurses into subdirectories
			paths.forEach(System.out::println);
		}
	}
}
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.