的Javadoc
FileSystem#getPathMatcher()有一些非常好的示例和解释
*.java Matches a path that represents a file name ending in .java *.* Matches file names containing a dot*.{java,class} Matches file names ending with .java or .class foo.?Matches file names starting with foo. and a single character extension /home/*/* Matches /home/gus/data on UNIX platforms /home/** Matches /home/gus and /home/gus/data on UNIX platforms C:\*Matches C:foo and C:bar on the Windows platform (note that the backslash is escaped; as a string literal in the Java Language the pattern would be "C:\\*")因此
/home/**将匹配
/home/gus/data,但
/home/*不会。
/home/*直接说
/home目录中的每个文件。
/home/**说里面的任何目录的每个文件
/home。
的例子
*VS
**。假设您当前的工作目录为
/Users/username/workspace/myproject,则以下内容仅与
./myproject文件(目录)匹配。
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:/Users/username/workspace/*");Files.walk(Paths.get(".")).forEach((path) -> { path = path.toAbsolutePath().normalize(); System.out.print("Path: " + path + " "); if (pathMatcher.matches(path)) { System.out.print("matched"); } System.out.println();});如果使用
**,它将匹配该目录中的所有文件夹和文件。



