Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在多个文件夹中搜索文件的最快方法_Java_File - Fatal编程技术网

Java 在多个文件夹中搜索文件的最快方法

Java 在多个文件夹中搜索文件的最快方法,java,file,Java,File,我需要根据包含3100多个文件和14个文件夹的目录中的文件名列表搜索文件,但完成搜索需要几个小时。此外,我只讨论了1个文件名列表,我还有其他文件名列表要搜索 找到要搜索的文件后,我需要访问它并搜索其中的单词。最后进入下一个文件 我现在正在做的是,我使用了广度优先搜索的概念,但完成搜索也需要几个小时 还有其他方法可以更快地完成此任务吗?请参阅代码中的注释 public class FileFinderApp { // Create a list of file names that you

我需要根据包含3100多个文件和14个文件夹的目录中的文件名列表搜索文件,但完成搜索需要几个小时。此外,我只讨论了1个文件名列表,我还有其他文件名列表要搜索

找到要搜索的文件后,我需要访问它并搜索其中的单词。最后进入下一个文件

我现在正在做的是,我使用了广度优先搜索的概念,但完成搜索也需要几个小时


还有其他方法可以更快地完成此任务吗?

请参阅代码中的注释

public class FileFinderApp {
    // Create a list of file names that you want to process
    private List<String> fileNames = new ArrayList<String>(Arrays.asList(new String[] {"test.txt","test1.txt","test2.txt"}));
    // Create a FolderFilter, this just allows us to find files that are actually folders
    private FolderFilter folderFilter = new FolderFilter();

    public FileFinderApp() {
        // Let the user know we are doing something in case there is no other output
        System.out.println("Finding Files");
        // Create a File to represent our starting folder
        File startFolder = new File("F:\\FileTest");
        // Get the list of Files that match our criteria 
        List<File> foundFiles = findFiles(startFolder, new FileNameFilter());
        // loop through our files and do something with them
        for (File file : foundFiles) {
            // do something with each file
            System.out.println(file.toString());
        }
        // Let the user know we are done.
        System.out.println("Done Finding Files");
    }

    // This filter returns true if the File is a file (not folder, etc.) and matches one of our file names. 
    class FileNameFilter implements FileFilter {
        public boolean accept(File file) {
            return fileNames.contains(file.getName()) && file.isFile();
        }
    }

    // This filter only returns folders
    class FolderFilter implements FileFilter {
        public boolean accept(File file) {
            return file.isDirectory();
        }
    }

    // Here's the meat and potatoes
    private List<File> findFiles(File folder, FileFilter filter) {
        // Create an empty list of Files
        List<File> foundFiles = new ArrayList<File>();
        // Find all sub-folders
        File[] folders = folder.listFiles(folderFilter);
        // Find the folders that pass our filter
        File[] files = folder.listFiles(filter);
        // add the files to our found files
        foundFiles.addAll(Arrays.asList(files));
//      for (File file : files) {
//          System.out.println(file.getAbsolutePath());
//      }
        // loop through our sub-folders and get any files that match our filter there and add them to our list
        // This is recursive and will execute as many levels as there are nested folders
        for(File subFolder : folders) {
            foundFiles.addAll(findFiles(subFolder, filter));
        }
        return foundFiles;
    }

    public static void main(String[] args) {
        // don't block the UI
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new FileFinderApp();
            }
        });
    }
}
公共类FileFinderApp{
//创建要处理的文件名列表
私有列表文件名=新的ArrayList(Arrays.asList(新字符串[]{“test.txt”、“test1.txt”、“test2.txt”}));
//创建一个FolderFilter,这只允许我们查找实际上是文件夹的文件
私有FolderFilter FolderFilter=新FolderFilter();
公共文件finderapp(){
//让用户知道我们正在做一些事情,以防没有其他输出
System.out.println(“查找文件”);
//创建一个文件来表示我们的起始文件夹
File startFolder=新文件(“F:\\FileTest”);
//获取符合条件的文件列表
List foundFiles=findFiles(startFolder,new FileNameFilter());
//循环浏览我们的文件并对其进行处理
用于(文件:foundFiles){
//对每个文件执行一些操作
System.out.println(file.toString());
}
//让用户知道我们已经完成了。
System.out.println(“完成查找文件”);
}
//如果文件是一个文件(不是文件夹等),并且与我们的一个文件名匹配,则此筛选器返回true。
类FileNameFilter实现FileFilter{
公共布尔接受(文件){
返回fileNames.contains(file.getName())和&file.isFile();
}
}
//此筛选器仅返回文件夹
类FolderFilter实现FileFilter{
公共布尔接受(文件){
返回文件.isDirectory();
}
}
//这是肉和土豆
私有列表FindFile(文件文件夹、文件筛选器){
//创建一个空的文件列表
List foundFiles=new ArrayList();
//查找所有子文件夹
File[]folders=folder.listFiles(folderFilter);
//查找通过筛选的文件夹
File[]files=文件夹.listFiles(过滤器);
//将这些文件添加到我们找到的文件中
addAll(Arrays.asList(files));
//用于(文件:文件){
//System.out.println(file.getAbsolutePath());
//      }
//循环浏览我们的子文件夹,获取与我们的过滤器匹配的任何文件,并将它们添加到我们的列表中
//这是递归的,执行的级别与嵌套文件夹的级别相同
用于(文件子文件夹:文件夹){
addAll(findFiles(子文件夹,过滤器));
}
归还文件;
}
公共静态void main(字符串[]args){
//不要阻塞用户界面
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
新文件finderApp();
}
});
}
}
参见注释:

/**
 * @param searchIn
 * a valid path to any non null file or directory
 * @param searchFor
 * any non null name of a file or directory
 */
public static File findFile(String searchIn, String searchFor){

    return findFile(new File(searchIn), searchFor);
}

/**
 * @param searchIn
 * not null file or directory
 * @param searchFor
 * any non null name of a file or directory
 */
public static File findFile(File searchIn, String searchFor){

    if(searchIn.getName().equalsIgnoreCase(searchFor)) {
        return searchIn;
    }

    if(searchIn.isDirectory() && (searchIn.listFiles() != null)){

        for (File file : searchIn.listFiles() ){
            File f = findFile(file, searchFor);
            if(f != null) {
                return f;
            }
        }
    }

    return null;
}
通过以下方式进行测试:

    String sourcePath = "C:\\";
    String searchFor = "Google Docs.ico";
    File f = findFile(sourcePath, searchFor);
衡量的绩效:

时间:13.596946秒,目录:17985文件:116837


听起来慢得离谱。那是什么代码?你能给我们看一个例子吗?如果你能用手比用电脑更快地完成一项任务(每个文件可能需要几秒钟的时间),那么你的算法就大错特错了。请发帖子哦,我的错,我是在问一些建议,而不是代码。我把问题贴错页面了吗?
SwingUtilities