How to find files that match a wildcard string in Java?

前端 未结 16 1270
慢半拍i
慢半拍i 2020-11-22 10:52

This should be really simple. If I have a String like this:

../Test?/sample*.txt

then what is a generally-accepted way to get a list of fil

16条回答
  •  天命终不由人
    2020-11-22 11:19

    Since Java 8 you can use Files#find method directly from java.nio.file.

    public static Stream find(Path start,
                                    int maxDepth,
                                    BiPredicate matcher,
                                    FileVisitOption... options)
    

    Example usage

    Files.find(startingPath,
               Integer.MAX_VALUE,
               (path, basicFileAttributes) -> path.toFile().getName().matches(".*.pom")
    );
    

提交回复
热议问题