Java Recursive Fuzzy Query File Instance Code

  • 2021-12-11 18:03:59
  • OfStack

Directory Preface Java Recursive Fuzzy Query File Summary

Preface

In the design of data structure algorithm, or the concrete implementation of a method, there is a method called "recursion", which is not particularly difficult in thought, but there are still some things to pay attention to in implementation. Although many recursive algorithms can be replaced by corresponding loop iteration, it is difficult to understand and implement some abstract and complex algorithms without recursion.

Java Recursive Fuzzy Query File

String fuzzy query


    /**
     *  Fuzzy query 
     * @param str           String to query 
     * @param part          Part 
     * @return  true  What the representative found   false  The representative didn't find it 
     */
    public boolean matchStringByIndexOf(String str,String part) {
        int count = 0;
        int index = 0;
        while( ( index = str.indexOf(part, index) ) != -1 )
        {
            index = index+part.length();
            count++;
        }
        if(count < 1){
            return false;
        }
        return true;
    }

Recursive traversal of files


    /**
     *  Fuzzy query related files 
     * @param path       File path 
     * @param fileName   Files to be found 
     */
    public List<String> searchFileList(File path, String fileName){
        File[] files=path.listFiles();   // List all subfiles 
        for(File file :files)
        {
            if(file.isFile()){// If it is a file, the query is fuzzy first , Determine whether it is relevant 
                if(stringUtilService.matchStringByIndexOf(file.toString(),fileName)){
                    fileListData.add(file.toString());
                }
            }else if(file.isDirectory())// If it is a folder, output the name of the folder and recursively traverse the folder 
            {
                searchFileList(file,fileName);// Recursive traversal 
            }
        }
        return fileListData;
    }

Test


    public static void main(String[] args) {
        String A ="G:/HTML/testData/ Da Vinci ";
        File file = new File(A);
        FileService fileService = new FileServiceImpl();
        System.out.println(fileService.searchFileList(file,"myse").toString());
    }

Summarize


Related articles: