Java Path & Files API
In this page:
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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());
}
}
Login to try C/C++/Java/PHP code in the editor
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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);
}
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: