In Java, path.endsWith(other) returns true when the last name elements of path are exactly the name elements of other. /home/anna/docs/report.txt ends with report.txt and with docs/report.txt, but not with port.txt.
Because whole names are compared, endsWith(".txt") is false for report.txt: ".txt" would have to be the complete last name. To test an extension, compare the String of the file name instead.
An instance method of the java.nio.file.Path interface, available since Java 7. Call it on a Path object, as in path.endsWith(...), after import java.nio.file.Path;.
Syntax
Pass a Path or a String:
Java
Path file = Paths.get("/home/anna/docs/report.txt");
boolean a = file.endsWith("docs/report.txt"); // true
boolean b = file.endsWith(".txt"); // false: not a whole name
boolean c = file.getFileName().toString().endsWith(".txt"); // true
Call
What it does
p.endsWith(Path other)
The last names of p equal all names of other.
p.endsWith(String other)
Converts the text with p.getFileSystem().getPath(other) first.
p.getFileName().toString().endsWith(".txt")
The right way to check an extension.
Since Java 7; the String overload is a default method from Java 11. An absolute other only matches when it equals the whole path.
Gives the last name, whose text you can test with String.endsWith.
String.endsWith
Character-based: "report.txt".endsWith("port.txt") is true.
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 — Test Different Endings of One Path
Check one file path against its file name, its last two names, a partial name, and two absolute arguments.
Java
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathEndsWithBasic {
public static void main(String[] args) {
Path p = Paths.get("/home/anna/docs/report.txt");
System.out.println("report.txt: " + p.endsWith("report.txt"));
System.out.println("docs/report.txt: " + p.endsWith("docs/report.txt"));
System.out.println("port.txt: " + p.endsWith("port.txt"));
System.out.println("/docs/report.txt: " + p.endsWith("/docs/report.txt"));
System.out.println("/home/anna/docs/report.txt: " + p.endsWith("/home/anna/docs/report.txt"));
}
}
Output
Run with java PathEndsWithBasic. The output is the same on Windows, macOS, and Linux.
One or more trailing names. report.txt matches the last element, and docs/report.txt matches the last two, so both are true.
Partial names never match. port.txt is only the end of the characters of report.txt, not a complete name element, so the answer is false.
Absolute means the whole path. An argument with a root can only match the entire path: /docs/report.txt fails, while the full /home/anna/docs/report.txt succeeds.
Example 2 — Find Config Files by Their Last Folder and Name
Search a list for files called app.properties that sit in a folder named conf. A plain text check also matches a folder called myconf.
A two-name ending.Paths.get("conf", "app.properties") describes the ending to look for: a folder named conf directly containing app.properties.
myconf is not conf. The text of the second path ends with the characters conf/app.properties, but its folder name is myconf, so only the Path check rejects it.
The last name must match exactly. app.properties.bak is a different name from app.properties, so both checks are false for the backup file.
Example 3 — Avoid the endsWith(".txt") Trap
Count the text files in a list twice: once with Path.endsWith(".txt"), which never matches, and once with the file name as a String.
Java
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathEndsWithExtension {
public static void main(String[] args) {
Path[] files = {Paths.get("notes/todo.txt"), Paths.get("notes/plan.TXT"),
Paths.get("photos/cat.png"), Paths.get("notes/.txt")};
int wrong = 0;
int right = 0;
for (Path f : files) {
if (f.endsWith(".txt")) {
wrong++;
}
if (f.getFileName().toString().toLowerCase().endsWith(".txt")) {
right++;
}
}
System.out.println("Path.endsWith(\".txt\") matches: " + wrong);
System.out.println("file name check matches: " + right);
}
}
Output
Run with java PathEndsWithExtension. The output is the same on Windows, macOS, and Linux.
Path.endsWith(".txt") matches: 1
file name check matches: 3
How It Works
Only a file literally named .txt.Path.endsWith(".txt") compares the last whole name, so it matches just notes/.txt and misses todo.txt and plan.TXT.
Test the name as text.getFileName().toString() gives todo.txt, plan.TXT, and so on, and String.endsWith checks the characters at the end.
Ignore case for extensions.toLowerCase() lets plan.TXT count as a text file; the odd name .txt also counts, so filter out names that start with a dot if that matters.
Frequently Asked Questions
Path.endsWith compares whole name elements. ".txt" would have to be the complete last name. Use path.getFileName().toString().endsWith(".txt") to check an extension.
Call path.endsWith("report.txt") or path.endsWith(Paths.get("docs", "report.txt")). The names must match completely.
An absolute argument only matches if it is equal to the whole path. For a relative path it never matches.
Yes on Linux and macOS; on Windows the names are compared without regard to case.
No. It compares the path text only, so the file does not need to exist.