Finding Files

如果您曾经使用过 Shell 脚本,则很可能已使用 Pattern 匹配来定位文件。实际上,您可能已经广泛使用了它。如果未使用过,则 Pattern 匹配使用特殊字符来创建 Pattern,然后可以将文件名与该 Pattern 进行比较。例如,在大多数 shell 脚本中,星号*匹配任意数量的字符。例如,以下命令列出了当前目录中以.html结尾的所有文件:

% ls *.html

java.nio.file软件包为该有用功能提供了编程支持。每个文件系统实现都提供一个PathMatcher。您可以使用FileSystem类中的getPathMatcher(String)方法来检索文件系统的PathMatcher。以下代码片段获取默认文件系统的路径匹配器:

String pattern = ...;
PathMatcher matcher =
    FileSystems.getDefault().getPathMatcher("glob:" + pattern);

传递给getPathMatcher的字符串 参数指定语法风格和要匹配的 Pattern。本示例指定* glob *语法。如果您不熟悉 glob 语法,请参阅什么是地球

Glob 语法易于使用且灵活,但是,如果您愿意,还可以使用正则表达式或* regex *语法。有关正则表达式的更多信息,请参见Regular Expressions类。某些文件系统实现可能支持其他语法。

如果要使用其他形式的基于字符串 的 Pattern 匹配,则可以创建自己的PathMatcher类。此页面中的示例使用 glob 语法。

创建PathMatcher实例后,即可将文件与之匹配。 PathMatcherinterface只有一个方法matches,该方法接受Path参数并返回一个布尔值:它与 Pattern 匹配,否则不匹配。以下代码段查找以.java.class结尾的文件,并将这些文件打印到标准输出:

PathMatcher matcher =
    FileSystems.getDefault().getPathMatcher("glob:*.{java,class}");

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

递归 Pattern 匹配

搜索与特定 Pattern 匹配的文件与遍历文件树是紧密结合的。您知道多少次文件在文件系统上“某处”,但是在哪里?或者,也许您需要在文件树中找到具有特定文件 extensions 的所有文件。

Find示例正是这样做的。 Find与 UNIX findUtil 相似,但是在功能上有所减少。您可以扩展此示例以包括其他功能。例如,findUtil 支持-prune标志以从搜索中排除整个子树。您可以通过在preVisitDirectory方法中返回SKIP_SUBTREE来实现该功能。要实现符号链接后面的-L选项,可以使用四参数walkFileTree方法并传入FOLLOW_LINKS枚举(但请确保在visitFile方法中测试圆形链接)。

要运行“查找”应用程序,请使用以下格式:

% java Find <path> -name "<glob_pattern>"

该 Pattern 放在引号内,因此 Shell 不解释任何通配符。例如:

% java Find . -name "*.html"

这是Find示例的源代码:

/**
 * Sample code that finds files that match the specified glob pattern.
 * For more information on what constitutes a glob pattern, see
 * https://docs.oracle.com/javase/tutorial/essential/io/fileOps.html#glob
 *
 * The file or directories that match the pattern are printed to
 * standard out.  The number of matches is also printed.
 *
 * When executing this application, you must put the glob pattern
 * in quotes, so the shell will not expand any wild cards:
 *              java Find . -name "*.java"
 */

import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
import static java.nio.file.FileVisitResult.*;
import static java.nio.file.FileVisitOption.*;
import java.util.*;

public class Find {

    public static class Finder
        extends SimpleFileVisitor<Path> {

        private final PathMatcher matcher;
        private int numMatches = 0;

        Finder(String pattern) {
            matcher = FileSystems.getDefault()
                    .getPathMatcher("glob:" + pattern);
        }

        // Compares the glob pattern against
        // the file or directory name.
        void find(Path file) {
            Path name = file.getFileName();
            if (name != null && matcher.matches(name)) {
                numMatches++;
                System.out.println(file);
            }
        }

        // Prints the total number of
        // matches to standard out.
        void done() {
            System.out.println("Matched: "
                + numMatches);
        }

        // Invoke the pattern matching
        // method on each file.
        @Override
        public FileVisitResult visitFile(Path file,
                BasicFileAttributes attrs) {
            find(file);
            return CONTINUE;
        }

        // Invoke the pattern matching
        // method on each directory.
        @Override
        public FileVisitResult preVisitDirectory(Path dir,
                BasicFileAttributes attrs) {
            find(dir);
            return CONTINUE;
        }

        @Override
        public FileVisitResult visitFileFailed(Path file,
                IOException exc) {
            System.err.println(exc);
            return CONTINUE;
        }
    }

    static void usage() {
        System.err.println("java Find <path>" +
            " -name \"<glob_pattern>\"");
        System.exit(-1);
    }

    public static void main(String[] args)
        throws IOException {

        if (args.length < 3 || !args[1].equals("-name"))
            usage();

        Path startingDir = Paths.get(args[0]);
        String pattern = args[2];

        Finder finder = new Finder(pattern);
        Files.walkFileTree(startingDir, finder);
        finder.done();
    }
}

走文件树中介绍了递归遍历文件树。