查找文件

原文: https://docs.oracle.com/javase/tutorial/essential/io/find.html

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

  1. % ls *.html

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

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

传递给getPathMatcher的字符串参数指定语法 flavor 和要匹配的模式。此示例指定 glob 语法。如果您不熟悉 glob 语法,请参阅什么是 Glob

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

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

一旦创建了PathMatcher实例,就可以将文件与其匹配。 PathMatcher接口有一个方法, matches ,它接受一个Path参数并返回一个布尔值:它与模式匹配,或者不匹配。以下代码片段查找以.java.class结尾的文件,并将这些文件打印到标准输出:

  1. PathMatcher matcher =
  2. FileSystems.getDefault().getPathMatcher("glob:*.{java,class}");
  3. Path filename = ...;
  4. if (matcher.matches(filename)) {
  5. System.out.println(filename);
  6. }

搜索与特定模式匹配的文件与遍历文件树同时进行。你知道文件系统中的某个文件有多少次,但在哪里?或者您可能需要在文件树中找到具有特定文件扩展名的所有文件。

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

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

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

模式放在引号内,因此 shell 不会解释任何通配符。例如:

  1. % java Find . -name "*.html"

以下是Find示例的源代码:

  1. /**
  2. * Sample code that finds files that match the specified glob pattern.
  3. * For more information on what constitutes a glob pattern, see
  4. * https://docs.oracle.com/javase/tutorial/essential/io/fileOps.html#glob
  5. *
  6. * The file or directories that match the pattern are printed to
  7. * standard out. The number of matches is also printed.
  8. *
  9. * When executing this application, you must put the glob pattern
  10. * in quotes, so the shell will not expand any wild cards:
  11. * java Find . -name "*.java"
  12. */
  13. import java.io.*;
  14. import java.nio.file.*;
  15. import java.nio.file.attribute.*;
  16. import static java.nio.file.FileVisitResult.*;
  17. import static java.nio.file.FileVisitOption.*;
  18. import java.util.*;
  19. public class Find {
  20. public static class Finder
  21. extends SimpleFileVisitor<Path> {
  22. private final PathMatcher matcher;
  23. private int numMatches = 0;
  24. Finder(String pattern) {
  25. matcher = FileSystems.getDefault()
  26. .getPathMatcher("glob:" + pattern);
  27. }
  28. // Compares the glob pattern against
  29. // the file or directory name.
  30. void find(Path file) {
  31. Path name = file.getFileName();
  32. if (name != null && matcher.matches(name)) {
  33. numMatches++;
  34. System.out.println(file);
  35. }
  36. }
  37. // Prints the total number of
  38. // matches to standard out.
  39. void done() {
  40. System.out.println("Matched: "
  41. + numMatches);
  42. }
  43. // Invoke the pattern matching
  44. // method on each file.
  45. @Override
  46. public FileVisitResult visitFile(Path file,
  47. BasicFileAttributes attrs) {
  48. find(file);
  49. return CONTINUE;
  50. }
  51. // Invoke the pattern matching
  52. // method on each directory.
  53. @Override
  54. public FileVisitResult preVisitDirectory(Path dir,
  55. BasicFileAttributes attrs) {
  56. find(dir);
  57. return CONTINUE;
  58. }
  59. @Override
  60. public FileVisitResult visitFileFailed(Path file,
  61. IOException exc) {
  62. System.err.println(exc);
  63. return CONTINUE;
  64. }
  65. }
  66. static void usage() {
  67. System.err.println("java Find <path>" +
  68. " -name \"<glob_pattern>\"");
  69. System.exit(-1);
  70. }
  71. public static void main(String[] args)
  72. throws IOException {
  73. if (args.length < 3 || !args[1].equals("-name"))
  74. usage();
  75. Path startingDir = Paths.get(args[0]);
  76. String pattern = args[2];
  77. Finder finder = new Finder(pattern);
  78. Files.walkFileTree(startingDir, finder);
  79. finder.done();
  80. }
  81. }

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