Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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_Multithreading_Io - Fatal编程技术网

Java 文件搜索高性能程序

Java 文件搜索高性能程序,java,multithreading,io,Java,Multithreading,Io,我有一个要求,那就是我必须编写一个高性能的文件搜索程序,该程序应该列出所有与提供的名称模式匹配的文件和文件夹,从最上面的文件夹开始,在子文件夹中递归搜索 程序可以是具有以下输入的命令行主类 要开始搜索的顶部文件夹。例如C:\MyFolders 要搜索的项目的类型。文件或文件夹或两者 搜索模式java正则表达式java.util.regex被接受为paatern 示例MFile.tx?将找到UMFile123.txt和AIIMFile.txs' 应用程序必须返回的超时(秒)。否则,它必须返回“无法

我有一个要求,那就是我必须编写一个高性能的文件搜索程序,该程序应该列出所有与提供的名称模式匹配的文件和文件夹,从最上面的文件夹开始,在子文件夹中递归搜索

程序可以是具有以下输入的命令行主类

要开始搜索的顶部文件夹。例如C:\MyFolders 要搜索的项目的类型。文件或文件夹或两者 搜索模式java正则表达式java.util.regex被接受为paatern

示例MFile.tx?将找到UMFile123.txt和AIIMFile.txs' 应用程序必须返回的超时(秒)。否则,它必须返回“无法完成操作”消息

我想出了另一个办法,那就是

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import com.sapient.test.fileSearch.FileSearch;

public class FilesearchMain {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int flag=0;
        System.out.println("Type Item to Search ");
        System.out.println("1 File");
        System.out.println("2 Folder ");
        System.out.println("3 Both");
        System.out.println("0 Exit");

        try{
        BufferedReader readType = new BufferedReader(new InputStreamReader(System.in));


        String searchType =readType.readLine();;

        System.out.println("Enter name of file to search ::");

        BufferedReader readName = new BufferedReader(new InputStreamReader(System.in));
        String fileName=readName.readLine();


        if(searchType==null && fileName==null){
            throw new Exception("Error Occured::Provide both the input Parameters");
        }
        validateInputs(searchType,fileName);
        FileSearch fileSearch = new FileSearch(searchType,fileName);
List resultList=fileSearch.findFiles();
        System.out.println(resultList);
        }catch(IOException io){
            System.out.println("Error Occured:: Check the input Parameters and try again");
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }

    private static void validateInputs(String searchType, String fileName) 
    throws Exception{
        if(!(searchType.equals("1") || searchType.equals("2") || searchType.equals("3")) ){
            throw new Exception("Error:: Item to search can be only 1 or 2 or 3");
        }
        if(searchType.equals("") || fileName.equals("")){
            System.out.println("Error Occured:: Check the input Parameters and try again");

        }

    }

}
另一个文件是

public class FileSearch {
    private String searchType;
    private String fileName;

    public FileSearch(){

    }
    public FileSearch(String sType,String fName){
        this.searchType=sType;
        this.fileName=fName;
    }

    public String getSearchType() {
        return searchType;
    }
    public void setSearchType(String searchType) {
        this.searchType = searchType;
    }
    public String getFileName() {
        return fileName;
    }
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
    public List findFiles(){

        File file = new File("C:\\MyFolders");

        return searchInDirectory(file);

    }
    //Assuming that files to search should contain the typed name by the user
    //
    private List searchInDirectory(File dirName){
        List<String> filesList = new ArrayList<String>();
        if(dirName.isDirectory()){
            File [] listFiles = dirName.listFiles();
            for(File searchedFile : listFiles){
                if(searchedFile.isFile() && searchedFile.getName().toUpperCase().contains(getFileName().toUpperCase())&& 
                        (getSearchType().equals("1") || getSearchType().equals("3") ) ){
                    filesList.add(searchedFile.getName());
                }else if(searchedFile.isDirectory() && searchedFile.getName().toUpperCase().contains(getFileName().toUpperCase())
                    &&  (getSearchType().equals("2") || getSearchType().equals("3") ) ){
                    filesList.add(searchedFile.getName());
                    searchInDirectory(searchedFile);
                }else{
                    searchInDirectory(searchedFile);
                }
            }
        }
        return filesList;
    }

}


Please advise is this approach is correct as per design..!
其中fileFilter可能看起来像这样

public class MyFileFilter implements FileFilter{

    public boolean accept(File pathname) {
        return fileNamePattern.matcher(pathname.getName()).find();
    }

}
这基本上保证了返回的文件列表将匹配FileFilter中的条件

现在,这里是语义,因为为了让listFiles方法工作,它仍然需要遍历所有文件

您可以尝试维护过滤器的单个实例,而不是在每次迭代中重新创建它,但是您需要分析算法之间的差异以及这可能带来的任何好处

另一方面,您可以部署某种类型的线程队列,其中每个线程负责检查给定目录的匹配,并对任何新的子目录进行排队。只是一个想法

可重用模式

这些是我所做的基本改变,但实际上,你必须决定它们是否有效

public static class PatternFileFilter implements FileFilter {

    private Pattern fileNamePattern;

    public PatternFileFilter(Pattern fileNamePattern) {

        this.fileNamePattern = fileNamePattern;

    }

    @Override
    public boolean accept(File pathname) {

        return fileNamePattern.matcher(pathname.getName()).find() || pathname.isDirectory();

    }

    public Pattern getPattern() {
        return fileNamePattern;
    }
}

public static void searchFile(File topFolderOrFile, String type, PatternFileFilter filter, long timeOut) throws IOException {

    long startTimeStamp = Calendar.getInstance().getTimeInMillis();

    if (topFolderOrFile.isDirectory()) {

        File[] subFoldersAndFileNames = topFolderOrFile.listFiles(filter);
        if (subFoldersAndFileNames != null && subFoldersAndFileNames.length > 0) {
            for (File subFolderOrFile : subFoldersAndFileNames) {

                if (ITEM_TYPE_FILE.equals(type) && subFolderOrFile.isFile()) {
                    System.out.println("File name matched ----- "
                            + subFolderOrFile.getName());
                }
                if (ITEM_TYPE_FOLDER.equals(type)
                        && subFolderOrFile.isDirectory() && filter.getPattern().matcher(subFolderOrFile.getName()).find()) {
                    System.out.println("Folder name matched ----- "
                            + subFolderOrFile.getName());
                }
                if (ITEM_TYPE_FILE_AND_FOLDER.equals(type) && filter.getPattern().matcher(subFolderOrFile.getName()).find()) {
                    System.out.println("File or Folder name matched ----- "
                            + subFolderOrFile.getName());
                }

                // You need to decide if you want to process the folders inline
                // or after you've processed the file list...

                if (subFolderOrFile.isDirectory()) {
                    long timeElapsed = startTimeStamp
                            - Calendar.getInstance().getTimeInMillis();
                    if (((timeOut * 1000) - timeElapsed) < 0) {
                        System.out
                                .println("Could not complete operation-- timeout");
                    } else {
                        searchFile(subFolderOrFile, type,
                                filter, (timeOut * 1000)
                                - timeElapsed);
                    }
                }
            }

        }

    }

}

public static void searchFile(String topFolderName, String type, String fileNamePatternRegExp, long timeOut) throws IOException {

    File topFolderOrFile = new File(topFolderName);
    Pattern fileNamePattern = Pattern.compile(fileNamePatternRegExp);

    searchFile(topFolderOrFile, type, new PatternFileFilter(fileNamePattern), timeOut);

}

我只想说,这里有一条鱼,现在你需要学会钓鱼

您选择不使用File.listFilesFileFilter有什么原因吗?@MadProgrammer您能纠正我更新帖子的错误吗@user1577449:他正确地建议,在你自己重新发明一个内置函数之前,你应该对它进行基准测试,看看它是否足够好。下面是一个例子:。或者这个:伙计们,请先看看我的要求。。!!帮我把这个solution@user1577449是的,但是你需要决定什么有用,什么不有用…@MadProgrammer.请你像我一样发布完整的代码,这将是一个很大的帮助,让我的理解更清楚。。!请像我所做的那样发布oplete代码,这将使理解从一开始就更加清晰,非常感谢
public static void searchFile(String topFolderName, String type,
        String fileNamePatternRegExp, long timeOut) throws IOException {

    long startTimeStamp = Calendar.getInstance().getTimeInMillis();

    File topFolderOrFile = new File(topFolderName);
    Pattern fileNamePattern = Pattern.compile(fileNamePatternRegExp);

    searchFile(topFolderName, type, fileNamePattern, long timeOut);

}

public static void searchFile(String topFolderName, String type,
        Pattern fileNamePattern, long timeOut) throws IOException {
    //...
}
public static class PatternFileFilter implements FileFilter {

    private Pattern fileNamePattern;

    public PatternFileFilter(Pattern fileNamePattern) {

        this.fileNamePattern = fileNamePattern;

    }

    @Override
    public boolean accept(File pathname) {

        return fileNamePattern.matcher(pathname.getName()).find() || pathname.isDirectory();

    }

    public Pattern getPattern() {
        return fileNamePattern;
    }
}

public static void searchFile(File topFolderOrFile, String type, PatternFileFilter filter, long timeOut) throws IOException {

    long startTimeStamp = Calendar.getInstance().getTimeInMillis();

    if (topFolderOrFile.isDirectory()) {

        File[] subFoldersAndFileNames = topFolderOrFile.listFiles(filter);
        if (subFoldersAndFileNames != null && subFoldersAndFileNames.length > 0) {
            for (File subFolderOrFile : subFoldersAndFileNames) {

                if (ITEM_TYPE_FILE.equals(type) && subFolderOrFile.isFile()) {
                    System.out.println("File name matched ----- "
                            + subFolderOrFile.getName());
                }
                if (ITEM_TYPE_FOLDER.equals(type)
                        && subFolderOrFile.isDirectory() && filter.getPattern().matcher(subFolderOrFile.getName()).find()) {
                    System.out.println("Folder name matched ----- "
                            + subFolderOrFile.getName());
                }
                if (ITEM_TYPE_FILE_AND_FOLDER.equals(type) && filter.getPattern().matcher(subFolderOrFile.getName()).find()) {
                    System.out.println("File or Folder name matched ----- "
                            + subFolderOrFile.getName());
                }

                // You need to decide if you want to process the folders inline
                // or after you've processed the file list...

                if (subFolderOrFile.isDirectory()) {
                    long timeElapsed = startTimeStamp
                            - Calendar.getInstance().getTimeInMillis();
                    if (((timeOut * 1000) - timeElapsed) < 0) {
                        System.out
                                .println("Could not complete operation-- timeout");
                    } else {
                        searchFile(subFolderOrFile, type,
                                filter, (timeOut * 1000)
                                - timeElapsed);
                    }
                }
            }

        }

    }

}

public static void searchFile(String topFolderName, String type, String fileNamePatternRegExp, long timeOut) throws IOException {

    File topFolderOrFile = new File(topFolderName);
    Pattern fileNamePattern = Pattern.compile(fileNamePatternRegExp);

    searchFile(topFolderOrFile, type, new PatternFileFilter(fileNamePattern), timeOut);

}