递归查找目录中的所有文本文件 [英] Recursively find all text files in directory

查看:118
本文介绍了递归查找目录中的所有文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取目录中所有文本文件的名称。如果目录有子目录,那么我也想在那些文件中获取任何文本文件。我不知道如何让任何数量的子目录继续进行。

I am trying to get the names of all of the text files in a directory. If the directory has subdirectories then I also want to get any text files in those as well. I am not sure how to make the process continue for any number of subdirectories.

现在,下面的代码只获取当前目录和子目录中的所有文本文件目录。对于找到的每个子目录,它还会查找任何文本文件和更深的子目录。问题是,如果那些更深层次的子目录还有更深的子目录,那么我找不到所有的文本文件。这似乎是一个需要递归的问题,因为我不知道这会有多深。

Right now the code below just gets all the text files in the current directory and and subdirectories in the directory. For each subdirectory found, it also finds any text files and deeper subdirectories. The problem is that if those deeper subdirectories have yet deeper subdirectories then I am not finding all the text files. This seems to be a problem that requires recursion because I don't know how deep this will go.

这是我到目前为止的代码:

Here is my code so far:

File rootDirectory = new File(rootDir);
if (rootDirectory.isDirectory()) {
    System.out.println("Valid directory");

    File[] listOfFiles = rootDirectory.listFiles(); 
    for (int i = 0; i < listOfFiles.length; i++) {
        String iName = listOfFiles[i].getName();
        if (listOfFiles[i].isFile()) {
            if (iName.endsWith(".txt") || iName.endsWith(".TXT")) {
                System.out.println("File: "+iName);
            }
        }
        if (listOfFiles[i].isDirectory()) {
            System.out.println("Directory: "+iName);

            File[] subList = listOfFiles[i].listFiles();
            for (int j = 0; j < subList.length; j++) {
                String jName = subList[j].getName();
                if (subList[j].isFile()) {
                    if (jName.endsWith(".txt") || jName.endsWith(".TXT")) {
                        System.out.println("\tFile: "+jName);
                    }
                }
                if (subList[j].isDirectory()) {
                    System.out.println("\tDirectory: "+jName);
                }
            }
        }
    }
}
else System.out.println("Invalid directory");

编辑:搞定了,谢谢Olaf Dietsche:

Edit: Got it working, thank you Olaf Dietsche:

public void findFiles(File root, int depth) {
    File[] listOfFiles = root.listFiles();
    for (int i = 0; i < listOfFiles.length; i++) {
        String iName = listOfFiles[i].getName();
        if (listOfFiles[i].isFile()) {
            if (iName.endsWith(".txt") || iName.endsWith(".TXT")) {
                for (int j = 0; j < depth; j++) System.out.print("\t");
                System.out.println("File: "+iName);
            }
        }
        else if (listOfFiles[i].isDirectory()) {
            for (int j = 0; j < depth; j++) System.out.print("\t");
            System.out.println("Directory: "+iName);
            findFiles(listOfFiles[i], depth+1);
        }
    }
}


推荐答案

这是递归问题

public void find_files(File root)
{
    File[] files = root.listFiles(); 
    for (File file : files) {
        if (file.isFile()) {
            ...
        } else if (file.isDirectory()) {
            find_files(file);
        }
    }
}

这篇关于递归查找目录中的所有文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆