何时在JAVA中使用glob语法中的**(双星)

时间:2022-09-01 23:09:34

Directly from this Java Oracle tutorial:

直接来自这个Java Oracle教程:

Two asterisks, **, works like * but crosses directory boundaries. This syntax is generally used for matching complete paths.

两个星号**的作用类似于*但跨越目录边界。此语法通常用于匹配完整路径。

Could anybody do a real example out of it? What do they mean with "crosses directory boundary"? Crossing the directory boundary, I imagine something like checking the file from root to getNameCount()-1. Again a real example explaining the difference between * and ** in practice would be great.

有人可以做一个真实的例子吗?他们对“十字架目录边界”有什么意义?穿过目录边界,我想象一下像从root用户检查文件到getNameCount() - 1。再次,一个真实的例子解释了*和**在实践中的区别将是伟大的。

1 个解决方案

#1


54  

The javadoc for FileSystem#getPathMatcher() has some pretty good examples and explanations

FileSystem#getPathMatcher()的javadoc有一些很好的例子和解释

*.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:\\\\*")  

So /home/** would match /home/gus/data, but /home/* wouldn't.

所以/ home / **会匹配/ home / gus / data,但/ home / *不会。

/home/* is saying every file directly in the /home directory.

/ home / *直接在/ home目录中说出每个文件。

/home/** is saying every file in any directory inside /home.

/ home / **表示/ home里面任何目录中的每个文件。


Example of * vs **. Assuming your current working directory is /Users/username/workspace/myproject, then the following will only match the ./myproject file (directory).

* 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();
});

If you use **, it will match all folders and files within that directory.

如果使用**,它将匹配该目录中的所有文件夹和文件。

#1


54  

The javadoc for FileSystem#getPathMatcher() has some pretty good examples and explanations

FileSystem#getPathMatcher()的javadoc有一些很好的例子和解释

*.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:\\\\*")  

So /home/** would match /home/gus/data, but /home/* wouldn't.

所以/ home / **会匹配/ home / gus / data,但/ home / *不会。

/home/* is saying every file directly in the /home directory.

/ home / *直接在/ home目录中说出每个文件。

/home/** is saying every file in any directory inside /home.

/ home / **表示/ home里面任何目录中的每个文件。


Example of * vs **. Assuming your current working directory is /Users/username/workspace/myproject, then the following will only match the ./myproject file (directory).

* 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();
});

If you use **, it will match all folders and files within that directory.

如果使用**,它将匹配该目录中的所有文件夹和文件。