简体   繁体   English

快速列出目录及其所有子目录中的文件

[英]Fast listing files in a directory and all of its subdirectories

I need to find all files in a directory and all of its subdirectories (except some). 我需要在目录及其所有子目录(某些目录除外)中找到所有文件。
Currently I'm using this method: 目前,我正在使用这种方法:

public  static Collection<File> listFiles(File directory,FilenameFilter filter,boolean recurse){
    Vector<File> files = new Vector<File>();
    File[] entries = directory.listFiles();
    if(entries!=null){
        for (File entry : entries){
            if (filter == null || filter.accept(directory, entry.getName())){
                    files.add(entry);
            }

            if (recurse && entry.isDirectory()){
                    files.addAll(listFiles(entry, filter, recurse));
            }
        }
    }
    return files;
}

and using it like this: 并像这样使用它:

        this.foundFiles=listFiles(new File(this.BaseDirectory), new FilenameFilter() {
            public boolean accept(File dir, String name) {
                boolean res=true;
                if(name.endsWith(".pdf")){
                    if(!dir.getPath().endsWith("done")){
                        if((workingFile!=null && (dir.getPath().equals(workingFile.getParent()) && name.equals(workingFile.getName())))){
                            res=false;
                        }else{
                            try {
                                ArrayList<String> AuthFolders = DB.getGroupAuthFoldersArray();
                                for(String folder:AuthFolders){
                                    if(dir.getPath().startsWith(BaseDirectory+File.separator+folder)){
                                        res=true;
                                        break;
                                    }else{
                                        res=false;
                                    }
                                }
                            } catch (SQLException ex) {
                                Logger.getLogger(scanner.class.getName()).log(Level.SEVERE, null, ex);
                                res=false;
                            } catch (InterruptedException ex) {
                                Logger.getLogger(scanner.class.getName()).log(Level.SEVERE, null, ex);
                                res=false;
                            }
                        }
                    }else{
                        res=false;
                    }
                }else{
                    res=false;
                }
                return res;
            }
        }, true);

But this is too slow! 但这太慢了! I have about 3000 files in the directory and it takes 10-15 mins (!!) for this method to find them all. 我的目录中大约有3000个文件,这种方法需要10到15分钟(!!)才能找到所有文件。

How Can I do this fast? 我该如何快速完成?
I'm thinking about using org.apache.commons.io.FileUtils.listfiles method. 我正在考虑使用org.apache.commons.io.FileUtils.listfiles方法。 Is there a faster way? 有没有更快的方法?

Thanks 谢谢

Use Files.walkFileTree in Java 7, which is faster than listFiles because it uses streaming. 使用Files.walkFileTree在Java 7中,这是快于listFiles因为它使用流媒体。 Read the tutorial for more information. 阅读教程以获取更多信息。

} catch (SQLException ex) { 

这不属于旨在执行目录列表的类!

暂无
暂无

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

相关问题 如何在hadoop hdfs中列出目录及其子目录中的所有文件 - How to list all files in a directory and its subdirectories in hadoop hdfs 搜索目录及其子目录中的所有 po 文件,并创建一个 Hashmap,其中包含文件路径作为值,其目录作为键 - Search for all po files in a directory and its subdirectories and create a Hashmap containing the filepaths as value and its directory as key 获取目录及其子目录中的文件数 - get number of files in a directory and its subdirectories 以递归方式获取Java中的目录及其子目录中的所有文件 - Non-recursive way to get all files in a directory and its subdirectories in Java 如何使用Java对文件夹及其所有文件和子目录进行zip / upzip? - How to zip/upzip a folder and all of its files and subdirectories using Java? 删除所有文件和子目录,但在Java中保持当前目录为空 - Delete all files and subdirectories but keep the current directory empty in Java 如何使用正则表达式获取目录中不包含子目录的所有文件? - How to get all files in a directory with exclusion of subdirectories using regular expression? 使用Java在Linux中列出子目录/文件 - listing subdirectories/files in linux using java 在 Clojure 中列出目录中的文件 - Listing files in a directory in Clojure 如何使用 FTP 上的骆驼路线将所有文件从目录(包括子目录)移动到目标中没有子目录的特定目录? - How to move all files from a directory(including subdirectories) to a specific directory without subdirectories in target using camel route over FTP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM