In Java, path.normalize() returns a cleaned-up copy of the path: every . ("this folder") is removed, and every .. ("parent folder") is removed together with the name in front of it. docs/./old/../report.txt becomes docs/report.txt.
It is purely textual. Java does not look at the disk, so the files do not need to exist, and symbolic links are not followed. A .. that has nothing in front of it stays in a relative path (../a.txt) and is dropped after a root (/../a.txt becomes /a.txt).
An instance method of the java.nio.file.Path interface, available since Java 7. Call it on a Path object, as in path.normalize(...), after import java.nio.file.Path;.
Syntax
Call it with no arguments; the original path is not changed:
Joins paths and leaves the .. for normalize to clean.
Java Examples
Outputs below are shown for Linux and macOS. On Windows, Java prints \ between names instead of /; the note above each output says when a result really differs.
Example 1 — Clean Up Paths with "." and ".."
Normalize five paths, from a typical messy relative path to cases where everything cancels out.
Java
import java.nio.file.Paths;
public class PathNormalizeBasic {
public static void main(String[] args) {
String[] inputs = {"docs/./reports/../report.txt", "./notes.txt", "../shared/a.txt",
"/home/anna/../../..", "a/b/../.."};
for (String s : inputs) {
System.out.println(s + " -> [" + Paths.get(s).normalize() + "]");
}
}
}
Output
Run with java PathNormalizeBasic. Shown for Linux and macOS. On Windows the results print with \, for example [docs\report.txt] and [\].
Dots vanish, pairs cancel. In the first path the . is dropped and reports cancels against the .. after it, leaving docs/report.txt.
A leading ".." has no partner. ../shared/a.txt points above the working directory; there is no name before .. to remove, so it is kept.
The root is a hard floor. /home/anna/../../.. tries to climb three levels from two names, so it stops at /; the relative a/b/../.. cancels completely and becomes the empty path, printed as [].
Example 2 — Compare Paths That Are Written Differently
Three spellings of the same source file are not equal until they are normalized. The name count shows what was removed.
Java
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathNormalizeCompare {
public static void main(String[] args) {
Path a = Paths.get("projects/shop/../shop/src/App.java");
Path b = Paths.get("projects/./shop/src/App.java");
Path c = Paths.get("projects/shop/src/App.java");
System.out.println("a.equals(c): " + a.equals(c));
System.out.println("b.equals(c): " + b.equals(c));
System.out.println("a normalized: " + a.normalize());
System.out.println("equal after normalize: " + (a.normalize().equals(c) && b.normalize().equals(c)));
System.out.println("names in a: " + a.getNameCount() + " -> " + a.normalize().getNameCount());
}
}
Output
Run with java PathNormalizeCompare. Shown for Linux and macOS. Windows prints the same paths with \ between the names.
a.equals(c): false
b.equals(c): false
a normalized: projects/shop/src/App.java
equal after normalize: true
names in a: 6 -> 4
How It Works
equals compares the text. a and b contain extra .. and . elements, so neither is equal to c, even though all three describe the same file.
Normalize both sides. After normalize(), shop/.. disappears from a and the dot from b, and all three paths are equal.
Two names fewer. a starts with six names; the cancelled pair shop and .. leaves four, which getNameCount() confirms.
Example 3 — See What Happens When ".." Climbs Too High
Relative paths keep the .. steps they cannot cancel; absolute paths cannot climb above the root.
Java
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathNormalizeAbove {
public static void main(String[] args) {
String[] inputs = {"../a.txt", "docs/../../b.txt", "/../a.txt", "/srv/../../c.txt"};
for (String s : inputs) {
Path n = Paths.get(s).normalize();
System.out.println(s + " -> " + n + " (absolute: " + n.isAbsolute() + ")");
}
}
}
Output
Run with java PathNormalizeAbove. Shown for Linux and macOS. On Windows the results print with \ and the last two are not absolute, because they have no drive letter.
Relative paths remember the climb. In docs/../../b.txt the first .. cancels docs and the second has no partner, so ../b.txt still points one level above the start.
Above the root is the root. For /../a.txt and /srv/../../c.txt every extra .. after the root is simply dropped, because nothing exists above /.
Leftover ".." is a warning sign. A normalized relative path that still starts with .. leaves its base folder; reject such input when it should stay inside a folder.
Frequently Asked Questions
It removes "." elements and removes each ".." together with the name in front of it. docs/./old/../a.txt becomes docs/a.txt.
No. It only works on the text of the path. Use toRealPath() to resolve the path against the disk.
In a relative path there is no name in front of it to remove, so the step up must be kept. After a root such as / it is removed, because nothing is above the root.
normalize is textual and never fails. toRealPath asks the file system, follows symbolic links, returns an absolute path, and throws IOException if the file does not exist.
It is one step. Resolve the user input against the base folder, normalize the result, and then check that it still startsWith the base. Use toRealPath for links.