How to check the extension of a Java 7 Path

前端 未结 4 1177
温柔的废话
温柔的废话 2020-12-08 03:23

I\'d like to check if a Path (introduced in Java 7) ends with a certain extension. I tried the endsWith() method like so:

Path path = Paths.get         


        
4条回答
  •  佛祖请我去吃肉
    2020-12-08 04:03

    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.

提交回复
热议问题