How to check the extension of a Java 7 Path

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

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

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

See the Finding Files tutorial for details.

Leave a Comment