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 OverflowJava 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.
The Path class does not have a notion of "extension", probably because the file system itself does not have it. Which is why you need to check its String representation and see if it ends with the four five character string .java. Note that you need a different comparison than simple endsWith if you want to cover mixed case, such as ".JAVA" and ".Java":
path.toString().toLowerCase().endsWith(".java");