Sub topic 1
Representing Locations with `Path`
Prerequisites
You should know basic Java classes and methods, String, and how to run a Java program. This chapter assumes Java 11 or later and basic familiarity with checked exceptions.
What a `Path` represents
A Path is an object that represents a location in a file system. It can describe a file or a directory, but creating a Path does not create anything on disk. A path can point to something that does not exist yet.
Use Path.of to create a path:
import java.nio.file.Path;
public class PathDemo {
public static void main(String[] args) {
Path relative = Path.of("data", "notes.txt");
Path absolute = Path.of("/var", "log", "app.log");
System.out.println(relative);
System.out.println(absolute);
System.out.println(relative.getFileName());
System.out.println(relative.getParent());
}
}The first path is relative. Its meaning depends on the program's current working directory. The second path is absolute because it starts at the file system root.
Path uses the separator appropriate for the operating system. On Unix-like systems, the separator is /. On Windows, it is usually \. Passing separate path parts to Path.of helps your program remain portable.
Combining and cleaning paths
Use resolve to append a child path to a base path:
import java.nio.file.Path;
public class PathOperations {
public static void main(String[] args) {
Path base = Path.of("data");
Path report = base.resolve("reports").resolve("today.txt");
Path messy = Path.of("data/../data/./notes.txt");
Path clean = messy.normalize();
Path project = Path.of("project");
Path source = Path.of("project", "src", "Main.java");
Path relativeToProject = project.relativize(source);
System.out.println(report); // data/reports/today.txt
System.out.println(clean); // data/notes.txt
System.out.println(relativeToProject); // src/Main.java
}
}normalize removes redundant . and .. elements using only the path text. It does not check whether the path exists and does not resolve symbolic links.
relativize calculates how to get from one path to another. The paths usually need compatible roots. For example, calculating a relative path between C:\work and /tmp is invalid because their roots differ.
| Operation | Purpose | Accesses the file system? |
|---|---|---|
| `resolve` | Appends one path to another | No |
| `normalize` | Removes redundant path elements | No |
| `relativize` | Calculates a path between two locations | No |
| `toRealPath` | Finds the actual existing path | Yes |
Common mistakes
Do not use string concatenation to build paths:
Path wrong = Path.of("data/" + "notes.txt");
Path right = Path.of("data").resolve("notes.txt");The first form may work on one operating system but assumes a particular separator. The second form delegates path construction to the platform.
Remember that a Path is not file content and is not proof that a file exists. Use methods from Files when you need to inspect or change the file system.
Exercise
What path does Path.of("archive").resolve("../notes.txt").normalize() produce?
Show answer
It produces notes.txt. The combined path is archive/../notes.txt, and normalize removes the matching directory and parent-directory elements.