Java Path endsWith() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Java 7+

Definition

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
CallWhat 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.

Library declarations
Java
// java.nio.file.Path (interface)
boolean endsWith(Path other)
boolean endsWith(String other)

Parameters

ParameterDescription
otherThe ending to test, as a Path or as a String path. Must not be null.

Return Value

Return ValueDescription
trueThe path ends with all name elements of other (equal paths also count).
falseOtherwise, including partial names such as port.txt or .txt.

Compare with Similar Methods

Similar checks:

MethodHow it differs
startsWith()Compares the first name elements and the root.
getFileName()Gives the last name, whose text you can test with String.endsWith.
String.endsWithCharacter-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"));
    }
}

How It Works

  1. One or more trailing names. report.txt matches the last element, and docs/report.txt matches the last two, so both are true.
  2. 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.
  3. 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.

Java
import java.nio.file.Path;
import java.nio.file.Paths;

public class PathEndsWithConfig {
    public static void main(String[] args) {
        Path wanted = Paths.get("conf", "app.properties");
        Path[] files = {Paths.get("/etc/shop/conf/app.properties"),
                        Paths.get("/opt/shop/myconf/app.properties"),
                        Paths.get("/opt/conf/app.properties.bak")};
        for (Path f : files) {
            boolean byPath = f.endsWith(wanted);
            boolean byText = f.toString().endsWith(wanted.toString());
            System.out.println(f + "\n  path=" + byPath + ", text=" + byText);
        }
    }
}

How It Works

  1. A two-name ending. Paths.get("conf", "app.properties") describes the ending to look for: a folder named conf directly containing app.properties.
  2. 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.
  3. 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);
    }
}

How It Works

  1. 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.
  2. Test the name as text. getFileName().toString() gives todo.txt, plan.TXT, and so on, and String.endsWith checks the characters at the end.
  3. 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.

Next: Path.equals()

Test whether two Path objects hold the same path text (not whether they point to the same file).

equals() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful