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

时间:2018-07-27 04:22:03

标签: java file

我需要基于包含3100多个文件和14个文件夹的目录中的文件名列表搜索文件,但是完成搜索需要花费数小时。此外,我只谈论1个文件名列表,我还有其他要搜索的文件名列表。

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

我目前正在做的是我使用了“广度优先搜索”的概念,但是完成搜索也要花费数小时。

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

2 个答案:

答案 0 :(得分:0)

查看代码中的注释

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();
            }
        });
    }
}

答案 1 :(得分:0)

查看评论:

/**
 * @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

相关问题