比创建文件列表更好的文件搜索算法 [英] Better file search algorithm than creating a list of files

查看:68
本文介绍了比创建文件列表更好的文件搜索算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我正在做的一个项目,我制作了一个 java 程序来搜索用户输入指定的文件.

For a project I'm doing I made a java program that searches for a file specified by user input.

代码开始在用户指定的基本目录中搜索(即:C:).它循环遍历此目录中的所有文件,检查文件名是否与用户给定的搜索词匹配,如果匹配,则将文件绝对路径添加到字符串中.如果文件是目录,则将其添加到列表中以供稍后处理.

The code starts searching in a base directory specified by the user (ie: C:). It loops through all the files in this directory checking if the filename matches the search term given by the user, if it does match, the files absolute path is added to a string. If the file is a directory it is added to a list to be dealt with later.

完成基本文件夹的搜索后,它将以相同的方式搜索/删除列表中的第一个目录(再次将找到的任何目录添加到列表中)并继续直到没有更多目录可供搜索.然后将找到的文件显示给用户.

When the base folder is finished being searched it will search/remove the first directory in the list in the same way (adding any directories found to the list once again) and continues until there are no more directories to search. Then displaying the files found to the user.

我的问题;有没有更好的方法来搜索文件?也许立即搜索目录而不是将它们添加到列表中?任何建议都会很棒,提前致谢!这是我的代码.

My question; is there a better way to search for files? Perhaps searching directories immediately instead of adding them to the list? Any advice would be awesome, thanks in advance! Here's my code.

public String SearchDir(File directory){
    this.directory = directory;
    do{
        File[] files = this.directory.listFiles();
        if(files != null){
            for(int i = 0; i < files.length; i++){

                // The current file.
                File currentFile = files[i];

                // The files name without extension and path
                // ie C:\Documents and Settings\myfile.file = myfile
                String fileName = this    .removeExtension(this.removePath(currentFile.getName()));


                // Don't search hidden files
                if(currentFile.isHidden()){
                    continue;
                }
                System.out.println(currentFile.getAbsolutePath());

                // Check if the user wanted a narrow search
                if(this.narrow){
                    // Narrow search = check if the file STARTS with the     string given.
                        if(fileName.toLowerCase().startsWith(this.fileName.toLowerCase())){
                    this.found += currentFile.getAbsolutePath() + '\n';
                    this.foundXTimes++;
                }
            }
            else{
                // Non-Narrow search = check for the given string ANYWHERE in the file name.
                if(fileName.toLowerCase().contains(this.fileName.toLowerCase())){
                    this.found += currentFile.getAbsolutePath() + '\n';
                    this.foundXTimes++;
                }
            }

                // If the file is a directory add it to the buffer to be     searched later.
                if(currentFile.isDirectory()){
                    this.directoriesToSearch.add(currentFile);
                }
            }

            if(!this.directoriesToSearch.isEmpty()){
                this.directory = this.directoriesToSearch.remove(0);    
            }
        }
    } while(!this.directoriesToSearch.isEmpty());

    if(!this.found.equals(""))
        return this.found;
    else
        return "x";
}

推荐答案

有两种算法.深度优先搜索和广度优先搜索.
http://en.wikipedia.org/wiki/Depth-first_search
http://en.wikipedia.org/wiki/Breadth-first_search

There are two algorithms. Depth first search and breadth first search.
http://en.wikipedia.org/wiki/Depth-first_search
http://en.wikipedia.org/wiki/Breadth-first_search

对于您的问题,这些算法的时间效率是 O(n).更好是不可能的.但是你可以构建二叉树.那么你的搜索效率是 O(logn).但首先,您必须留出时间来构建二叉树.如果只搜索一个,不要使用二叉树.

These algorithms time efficiency is O(n) for your question. Better is imposible. But you can build binary tree. Then your search's efficiency is O(logn). But firstly, you have to allow time for binary tree building. If you search only one, dont use binary tree.

这篇关于比创建文件列表更好的文件搜索算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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