简体   繁体   English

读取目录中的所有文件,包括其子目录

[英]Reading all files in a directory including its sub directories

This is how I set the path: 这是我设置路径的方式:

    dPath = dPath.replace("\\", "/");

    String iLen;
    String FileName;

    File iFolder = new File(dPath);
    File[] listOfFiles = iFolder.listFiles();

When searching: 搜索时:

    for (int i = 0; i < listOfFiles.length; i++) 
    {
        if (listOfFiles[i].isFile()) 
        {
            FileName = listOfFiles[i].getName();

            for(String s : iEndsWith)
            {
                if(FileName.toLowerCase().endsWith(s))
                {
                    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy h:mm aaa");
                    iLen = ReadableBytes(listOfFiles[i].length());

                    Object rowData[] = { FileName, listOfFiles[i].getAbsoluteFile(), sdf.format(listOfFiles[i].lastModified()), iLen };
                    iTableModel.addRow(rowData);

                    iTotalFiles ++;
                }
            }
        }
    }

That will only look for files in the given directory path, but not it's sub directories. 那只会在给定目录路径中查找文件,而不是子目录。 How can I change that? 我该如何改变?

If you're on Java 7, you can use FileVisitor : http://docs.oracle.com/javase/tutorial/essential/io/walk.html 如果您使用的是Java 7,则可以使用FileVisitorhttp : //docs.oracle.com/javase/tutorial/essential/io/walk.html

If not, just use a simple recursive version of your function. 如果没有,只需使用函数的简单递归版本即可。

您可以使用Apache Commons中的DirectoryWalker遍历目录层次结构。

Pass folder as Initial File which is to be searched 将文件夹作为要搜索的初始文件传递

File foldr = new File("c:/javaFolder");

public void addFilesToList(File folder) {
        File[] listofFiles = folder.listFiles();
        if (listofFiles != null) {
            for (File file : listofFiles) {
                if (file.isFile()) {


                } else
                    addFilesToList(file);

            }
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 列出目录中所有文件(包括子目录)的最有效方法是什么? - What is the most efficient way to list all of the files in a directory (including sub-directories)? 如何列出目录及其所有子目录中的所有文件 - How to list all files within a directory and all its sub-directories 通过目录及其子目录中的所有文件搜索存储在数组列表中的值 - searching for values stored in an arraylist through all the files within a directory and its sub directories 如何复制目录及其子目录,文件和zip文件 - How to copy a directory with its sub directories , files and zip files 列出文件夹内容,包括所有子目录和文件 - List Folder contents including all sub directories and files 如何从目录和子目录复制具有某些扩展名的所有文件? - How to copy all files with certain extensions from a directory and sub directories? Eclipse在没有文件的Antlr生成的代码目录上显示警告,但在其子目录上不显示警告 - Eclipse shows warning on Antlr generated code directory with no files, but not on its sub-directories 递归监视Java中的目录和所有子目录 - Recursively monitor a directory and all sub directories in java SimpleFileVisitor 遍历目录树以查找除两个子目录之外的所有 .txt 文件 - SimpleFileVisitor to walk a directory tree to find all .txt files except in two sub directories 在目录中搜索文件及其在Java中的子目录 - Search files in directory and its sub directory in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM