In Java, path.startsWith(other) returns true when path begins with the same root and the same whole name elements as other. /home/anna/docs starts with /home/anna and with /, but not with /home/an, because an is not a complete name.
It compares names, not characters, and it does not touch the disk. An absolute path never starts with a relative one, so /home/anna does not start with home.
An instance method of the java.nio.file.Path interface, available since Java 7. Call it on a Path object, as in path.startsWith(...), after import java.nio.file.Path;.
Syntax
Pass another Path or a String that is converted to a path first:
Java
Path file = Paths.get("/home/anna/docs/report.txt");
boolean a = file.startsWith("/home/anna"); // true
boolean b = file.startsWith(Paths.get("/home/an")); // false: not a whole name
Call
What it does
p.startsWith(Path other)
Same root and the first names of p equal all names of other.
p.startsWith(String other)
Shortcut for p.startsWith(p.getFileSystem().getPath(other)).
p.normalize().startsWith(base)
Inside-the-folder check after removing ...
Since Java 7. The String overload became a default method in Java 11; the behaviour is unchanged.
After a successful check, gives the part below the prefix.
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 — Compare Path.startsWith with String.startsWith
Test one absolute path against a whole folder, a partial name, a relative prefix, and the root, then repeat the partial test on the text.
Run with java PathStartsWithBasic. Shown for Linux and macOS. On Windows the first four lines are the same, but the last one is false, because toString() returns \home\anna\... there.
Whole names only. /home/anna matches the first two elements exactly; /home/an fails because an is compared with anna as a complete name.
Roots must match too. home/anna has the right names but no root, so it is not a prefix of an absolute path; the bare root / is a prefix of every absolute path.
Strings compare characters. The last line uses String.startsWith, which happily matches half a folder name: the reason to prefer the Path method for folder checks.
Example 2 — Keep User File Names Inside an Upload Folder
Join untrusted names onto a base folder, clean them with normalize(), and accept only results that still start with the base. This blocks path traversal tricks such as ../../etc/passwd.
Java
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathStartsWithTraversal {
public static void main(String[] args) {
Path base = Paths.get("/srv/uploads");
String[] requests = {"cat.png", "albums/2026/dog.png", "../../etc/passwd",
"albums/../../secret.txt", "/etc/passwd"};
for (String r : requests) {
Path target = base.resolve(r).normalize();
if (target.startsWith(base)) {
System.out.println("OK " + r + " -> " + base.relativize(target));
} else {
System.out.println("REJECTED " + r);
}
}
}
}
Output
Run with java PathStartsWithTraversal. Shown for Linux and macOS. On Windows the accepted paths print with \; the same requests are accepted and rejected.
OK cat.png -> cat.png
OK albums/2026/dog.png -> albums/2026/dog.png
REJECTED ../../etc/passwd
REJECTED albums/../../secret.txt
REJECTED /etc/passwd
How It Works
Join, then clean.resolve glues each request onto /srv/uploads and normalize cancels every .., so ../../etc/passwd turns into /etc/passwd before it is checked.
Hidden escapes are caught. albums/../../secret.txt looks harmless, but after cleaning it becomes /srv/secret.txt, which no longer starts with the base, so it is rejected.
Absolute input replaces the base.resolve returns an absolute argument unchanged, so /etc/passwd is also rejected; accepted names are printed relative to the base. Symbolic links inside the folder need an extra toRealPath() check.
Example 3 — Pick Only the Files Inside src/test
A text check would also match src/testdata. Compare it with the Path check while filtering a list of project files.
Same separators on both sides. The text check compares f.toString() with tests.toString(), so both use the native separator and the comparison is fair on every system.
The false positive. src/testdata/users.csv begins with the characters src/test, so the text check says true, while the Path check sees testdata is not test.
Any depth below the prefix. Fake.java sits one folder deeper, in src/test/util, and still starts with src/test; only the first two names have to match.
Frequently Asked Questions
Path.startsWith compares whole name elements and the root, so /home/anna does not start with /home/an. String.startsWith compares characters and would return true.
Normalize the path and call path.startsWith(folder), with both paths absolute or both relative. For files on disk, compare toRealPath() results so symbolic links cannot escape.
No. It compares the path as written. Call normalize() first, otherwise /base/../etc still starts with /base.
On Linux and macOS, yes. On Windows, names are compared without regard to case.
Yes. startsWith(String) converts the text to a path of the same file system first. It throws InvalidPathException if the text is not a valid path.