Sub topic 2
Combining and Cleaning Paths
This chapter assumes basic Java syntax, familiarity with String, and Java 11 or later. It also assumes you know that file operations can throw checked IOException exceptions. The examples build path descriptions, but they do not create, read, or delete files.
Building a path with `Path`
A Path represents a file or directory location. It can be absolute, such as /var/log/app.log, or relative, such as logs/app.log. A relative path is interpreted from the program's current working directory when it is used for file access.
Use Path.of to create one:
import java.nio.file.Path;
public class PathDemo {
public static void main(String[] args) {
Path base = Path.of("data");
Path file = base.resolve("notes.txt");
System.out.println(file);
System.out.println(file.getFileName());
}
}resolve appends one path to another. Here, file represents data/notes.txt. The separator is selected for the operating system, so the same Java code works on different platforms.
Combining paths with `resolve`
Use resolve instead of joining strings manually. String joining can produce duplicate separators or invalid paths.
Path project = Path.of("projects", "demo");
Path source = project.resolve("src").resolve("Main.java");The result is equivalent to projects/demo/src/Main.java.
An important rule is that an absolute argument replaces the path before it:
Path base = Path.of("data");
Path result = base.resolve(Path.of("/tmp/file.txt"));
System.out.println(result); // /tmp/file.txt on Unix-like systemsTherefore, validate or control child paths when they come from users or other untrusted sources. A child containing an absolute path does not stay under the intended base directory.
Cleaning paths with `normalize`
A path can contain . for the current directory and .. for its parent. normalize removes redundant elements:
import java.nio.file.Path;
public class NormalizeDemo {
public static void main(String[] args) {
Path path = Path.of("data/../data/./notes.txt");
System.out.println(path); // data/../data/./notes.txt
System.out.println(path.normalize()); // data/notes.txt
}
}normalize changes only the path object. It does not check whether the path exists and cannot resolve symbolic links. A path such as links/../file.txt may not refer to the expected location if links is a symbolic link. Use filesystem-aware methods such as toRealPath when that distinction matters.
Relating paths with `relativize`
relativize calculates how to travel from one path to another:
Path base = Path.of("projects", "demo");
Path target = Path.of("projects", "demo", "src", "Main.java");
System.out.println(base.relativize(target)); // src/Main.javaThe paths should normally both be relative or both be absolute. Absolute paths must also have compatible roots. For example, paths on different Windows drives cannot be relativized.
A practical sequence
For a path assembled from known components, use this sequence:
- Create the base with
Path.of. - Add children with
resolve. - Call
normalizewhen redundant elements are possible. - Use
relativizewhen displaying a path relative to a known directory. - Perform filesystem access only after the path has the intended meaning.
Exercise
What does Path.of("a", "b").resolve("../c").normalize() produce?
Show answer
It produces a path equivalent to a/c.