Implement a customizable search function

Source: Internet
Author: User
One time in my project, I wanted to search for the specified content from the user-specified directory. May be a directory or a file. and the name that the user is searching for may have a specific format. And can search for all subdirectories.





for a specific format search, we can do it with regular expressions. For example, all *.ini are file names with extension names, and their regular expressions are





. +\\.ini$











the poor polar search for subdirectories, you can use recursive functions that you have learned in Basic computer classes. You can see how to write in the following program.





designed a simple search framework for this purpose. The search function and the processing of search results are handled separately. This is also to achieve the highest flexibility. Its structure is as follows:


<pre>





+==========class===============+


+ filesearcher + +==========interface=======+


+==============================+ + Filesearcherlistener +


+ +search () + <>================o +==========================+


+ +registersearcherlistener () + + +found (file file) +


+ +unregistersearcherlistener () + +==========================+


+==============================+








</pre>


Through the class structure diagram above, we see that the Filesearcher class is the complete search function, which provides the search () method for searching. and Filesearcherlistener is responsible for processing the items that are searched. You can customize your search result processing by implementing the Filesearcherlistener interface. And through the Filesearcher class of Registersearcherlistener method to register your own definition of processing class. Such a structure, a good solution to the search process and search results to deal with the strong Lotus root and problems.


so I can easily customize what I want to search for and deal with. Take a look at the following example:


<pre>


/* Defines a listener for processing, an example of simply typing a filename in the console. */


Filesearcherlistener listener = new Filesearcherlistener ()


{


int count=1;


public void found (file file)


{


System.out.println ("Found" +count+ ":" +file);


count++;


}


};


/* Constructs the Filesearcher class, specifies that the search start directory is c:/winnt, the search type is file, and the matching format is all *.ini files. */


Filesearcher search = new Filesearcher ("C:/winnt", Filesearcher.file, ". +\\.ini$");


Search.registersearcherlistener (listener);


Search.search ();





</pre>














Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.