【问题标题】:How to find a file recursively in Java? [duplicate]如何在Java中递归查找文件? [复制]
【发布时间】:2016-11-11 04:01:27
【问题描述】:

我想知道如何编写一个递归程序来定位由起始路径指示的 Java 文件。它应该在树中搜索指定的文件。如果找到文件,则应返回文件的位置。这是我到目前为止所拥有的(不多,仍然需要基本清理)。我需要使用这些确切的方法。我最困惑的是什么方法。所以我知道我需要使用:

File f = new File(dirName);

String [] fileList = f.list();

File aFile = new File (dirName + "\\" + fileList[i]);

if (aFile.isDirectory()) {...}

public class FindFile {

如果你能帮我弄清楚这些方法中的每一个都是什么方法,那将是一个了不起的帮助!!!我只是不太了解每种方法的逻辑。我还有一个需要使用的另一个类的驱动程序。

/**
 * This constructor accepts the maximum number of files to find.
 */
public FindFile (int maxFiles)
{
}

/**
 * The parameters are the target file name to look for and the directory to start in.
 * @param  target = target file name, dirName = directory to start in
 */
public void directorySearch (String target, String dirName) {
    File f = new File(dirName);
    String [] fileList = f.list();
    File aFile = new File(dirName + "\\" + fileList[i]);
    if (aFile.isDirectory()) {
    }
    else {
    }
}

/**
 * This accessor returns the number of matching files found.
 * @return number of matching files found
 */
public int getCount () {
    return -1;
}

/**
 * This getter returns the array of file locations, up to maxFiles in size.
 * @return array of file locations
 */
public String [] getFiles () {
    return new String[] {""};
}

/**
 * Prompt the user for max number of files to look for, the directory to start in, and the file name.
 * Then, print out the list of found files with the full path name to the file (including starting
 * directory). In the event of an exception being thrown, driver catches it and provides an appropriate
 * message to the user.
 */
public static void main (String [] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("What is the max number of files to look for?");
    System.out.println("What directory should we start in?");
    Systme.out.println("What is the file name?");
    }

}

【问题讨论】:

    标签: java recursion


    【解决方案1】:

    您可以像这样使用 java 8 lambda 功能

    try (Stream<Path> walkStream = Files.walk(Paths.get("your search directory"))) {
        walkStream.filter(p -> p.toFile().isFile()).forEach(f -> {
            if (f.toString().endsWith("file to be searched")) {
                System.out.println(f + " found!");
            }
        });
    }
    

    【讨论】:

      【解决方案2】:

      你需要使用递归在所有文件、目录和子目录中搜索文件

      public static void main(String[] args) {
          boolean found = searchFile(new File("/tmp"), "10174");
          System.out.println(found);
      }
      
      private static boolean searchFile(File file, String search) {
          if (file.isDirectory()) {
              File[] files = file.listFiles();
              for (File f : files) {
                  boolean found = searchFile(f, search);
                  if (found)
                      return true;
              }
          } else {
              if (search.equals(file.getName())) {
                  return true;
              }
          }
          return false;
      }
      

      如果找到文件需要返回

      static File searchFile(File file, String search) {
          if (file.isDirectory()) {
              File[] arr = file.listFiles();
              for (File f : arr) {
                  File found = searchFile(f, search);
                  if (found != null)
                      return found;
              }
          } else {
              if (file.getName().equals(search)) {
                  return file;
              }
          }
          return null;
      }
      

      【讨论】:

      • 应该将其更改为返回找到的文件(或 null)而不是布尔值。
      • @Bill 是的,更新答案
      • 更新了描述。
      • @iloveprogramming 更新的答案就是你要找的,你试过了吗?第一个程序只返回布尔值,第二个程序返回 File 对象本身,File 有一个返回绝对路径的方法
      • 所以我希望驱动程序打印出找到的文件列表以及文件的完整路径名,包括起始目录。并返回找到的匹配文件的数量。
      猜你喜欢
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      • 2014-03-17
      • 2019-09-09
      • 2023-03-29
      • 1970-01-01
      • 2011-01-12
      相关资源
      最近更新 更多