Java NIO's PathMatcher provides FileSystem.getPathMatcher(String syntaxAndPattern):

PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:*.java");

Path filename = ...;
if (matcher.matches(filename.getFileName())) {
    System.out.println(filename);
}

.getFileName() is required when your path contains more than one component.

See the Finding Files tutorial for details.

Answer from fan on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › path-endswith-method-in-java-with-examples
Path endsWith() method in Java with Examples - GeeksforGeeks
April 13, 2023 - Exception: This method throws InvalidPathException If the path string cannot be converted to a Path. Below programs illustrate endsWith(String other) method: Program 1: ... // Java program to demonstrate // Path.endsWith(String other) method import java.nio.file.Path; import java.nio.file.Paths; public class GFG { public static void main(String[] args) { // create object of Path Path path = Paths.get("\\temp\\Spring"); // create a string object String passedPath = "Spring"; // call endsWith() to check path object // ends with passedPath or not boolean check = path.endsWith(passedPath); // print result System.out.println("Path ends with " + passedPath + " :" + check); } }
🌐
DZone
dzone.com › coding › languages › be careful with java path.endswith(string) usage
Be Careful with Java Path.endsWith(String) Usage
April 19, 2014 - // Match all jar files. Files.walk(dir).forEach(path -> { if (path.toString().endsWith(".jar")) System.out.println(path); }); With out the "toString()" you will spend many fruitless hours wonder why your program didn't work. Java (programming language) Strings Data Types Object (computer science)
🌐
Blogger
saltnlight5.blogspot.com › 2014 › 04 › be-careful-with-java-pathendswithstring.html
Zemian Blog: A Programmer's Journal: Be careful with Java Path.endsWith(String) usage
April 16, 2014 - Files.walk(dir).forEach(path -> { if (path.toString().endsWith(".jar")) System.out.println(path); }); With out the "toString()" you will spend many fruitless hours wonder why your program didn't work. ... Its actually a great and helpful piece of information. I am satisfied that you simply shared this helpful info with us. https://www.exltech.in/java-training.htmlReplyDelete
🌐
Java2s
java2s.com › Tutorials › Java › java.nio.file › Path › 0080__Path.endsWith_String_other_.htm
Java IO Tutorial - Java Path.endsWith(String other)
In the following code shows how to use Path.endsWith(String other) method. //w w w . jav a 2 s. c o m import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class Main { public static void main(String[] args) { Path path01 = Paths.get("/tutorial/Java/JavaFX/Topic.txt"); Path path02 = Paths.get("C:/tutorial/Java/JavaFX/Topic.txt"); boolean ew = path01.endsWith("Topic.txt"); System.out.println(ew); } }
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › nio › file › Path.html
Path (Java Platform SE 8 )
April 21, 2026 - Tests if this path ends with a Path, constructed by converting the given path string, in exactly the manner specified by the endsWith(Path) method. On UNIX for example, the path "foo/bar" ends with "foo/bar" and "bar". It does not end with "r" or "/bar".
🌐
Program Creek
programcreek.com › java-api-examples
java.nio.file.Path#endsWith
private static List<Archive> init() { List<Archive> result = new ArrayList<>(); Path home = Paths.get(System.getProperty("java.home")); try { if (home.endsWith("jre")) { // jar files in <javahome>/jre/lib result.addAll(addJarFiles(home.resolve("lib"))); } else if (Files.exists(home.resolve("lib"))) { // either a JRE or a jdk build image Path classes = home.resolve("classes"); if (Files.isDirectory(classes)) { // jdk build outputdir result.add(new JDKArchive(classes, ClassFileReader.newInstance(classes))); } // add other JAR files result.addAll(addJarFiles(home.resolve("lib"))); } else { throw new RuntimeException("\"" + home + "\" not a JDK home"); } return result; } catch (IOException e) { throw new Error(e); } }
🌐
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › nio › file › Path.html
Path (Java Platform SE 7 )
Tests if this path ends with a Path, constructed by converting the given path string, in exactly the manner specified by the endsWith(Path) method. On UNIX for example, the path "foo/bar" ends with "foo/bar" and "bar". It does not end with "r" or "/bar".
Find elsewhere
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › endsWith
Java String endsWith() - Check Suffix Match | Vultr Docs
December 17, 2024 - Here, endsWith() checks if the file path ends with either ".jpg" or ".jpeg", which confirms whether it's an image file format according to common standards.
🌐
Oracle
docs.oracle.com › en › java › javase › › 22 › docs › api › java.base › java › nio › file › Path.html
Path (Java SE 22 & JDK 22)
July 16, 2024 - Tests if this path ends with a Path, constructed by converting the given path string, in exactly the manner specified by the endsWith(Path) method. On UNIX for example, the path "foo/bar" ends with "foo/bar" and "bar". It does not end with "r" or "/bar".
🌐
Massapi
massapi.com › method › java › nio › file › Path.endsWith.html
Examples of java.nio.file.Path.endsWith() | massapi.com
If the given path has N elements, and no root component, and this path has N or more elements, then this path ends with the given path if the last N elements of each path, starting at the element farthest from the root, are equal.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.nio.filenio.ipath.endswith
IPath.EndsWith Method (Java.Nio.FileNio) | Microsoft Learn
Tests if this path ends with a Path, constructed by converting the given path string, in exactly the manner specified by the #endsWith(Path) endsWith(Path) method. On UNIX for example, the path "foo/bar" ends with "foo/bar" and "bar".
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › nio › file › Path.html
Path (Java SE 21 & JDK 21)
January 20, 2026 - Tests if this path ends with a Path, constructed by converting the given path string, in exactly the manner specified by the endsWith(Path) method. On UNIX for example, the path "foo/bar" ends with "foo/bar" and "bar". It does not end with "r" or "/bar".
🌐
Oracle
docs.oracle.com › en › java › javase › 20 › docs › api › java.base › java › nio › file › Path.html
Path (Java SE 20 & JDK 20)
July 10, 2023 - Tests if this path ends with a Path, constructed by converting the given path string, in exactly the manner specified by the endsWith(Path) method. On UNIX for example, the path "foo/bar" ends with "foo/bar" and "bar". It does not end with "r" or "/bar".
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › nio › file › Path.html
Path (Java SE 11 & JDK 11 )
January 20, 2026 - Tests if this path ends with a Path, constructed by converting the given path string, in exactly the manner specified by the endsWith(Path) method. On UNIX for example, the path "foo/bar" ends with "foo/bar" and "bar". It does not end with "r" or "/bar".
🌐
Oracle
docs.oracle.com › en › java › javase › 23 › docs › api › java.base › java › nio › file › Path.html
Path (Java SE 23 & JDK 23)
October 17, 2024 - Tests if this path ends with a Path, constructed by converting the given path string, in exactly the manner specified by the endsWith(Path) method. On UNIX for example, the path "foo/bar" ends with "foo/bar" and "bar". It does not end with "r" or "/bar".
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › io › pathOps.html
Path Operations (The Java™ Tutorials > Essential Java Classes > Basic I/O)
Path path = ...; Path otherPath = ...; Path beginning = Paths.get("/home"); Path ending = Paths.get("foo"); if (path.equals(otherPath)) { // equality logic here } else if (path.startsWith(beginning)) { // path begins with "/home" } else if (path.endsWith(ending)) { // path ends with "foo" }
🌐
Oracle
docs.oracle.com › en › java › javase › 13 › docs › api › java.base › java › nio › file › Path.html
Path (Java SE 13 & JDK 13 )
Tests if this path ends with a Path, constructed by converting the given path string, in exactly the manner specified by the endsWith(Path) method. On UNIX for example, the path "foo/bar" ends with "foo/bar" and "bar". It does not end with "r" or "/bar".
🌐
TutorialsPoint
tutorialspoint.com › java_nio › java_nio_path.htm
Java NIO - Path
compareTo(Path other) − Compares ... lexicographically greater than the argument. endsWith(Path other) − Tests if this path ends with the given path.If the given path has N elements, and no root component, and this path has ...