Java recursively searches for matching files under a specified folder

Source: Internet
Author: User
Tags readfile

ImportJava.io.File; Importjava.util.ArrayList; Importjava.util.List; ImportJava.util.Queue; /**  * @authorTiwson 2010-06-02 **/   Public classFilesearcher {/*** Find Files recursively *@parambasedirname Folder path to find *@paramtargetfilename file name to look for *@paramFileList found collection of files*/       Public Static voidfindfiles (String basedirname, String targetfilename, List fileList) {/*** Algorithm Description: * from a given folder to find all the subfolders and files of the folder, * if the file, then the match, the successful match is added to the result set, if it is a subfolder, then into the queue.          * Queue is not empty, repeat the above operation, the queue is empty, the program ends, return the result. */String tempname=NULL; //determine if a directory existsFile BaseDir =NewFile (basedirname); if(!basedir.exists () | | |basedir.isdirectory ()) {System.out.println ("File lookup failed:" + Basedirname + "is not a directory! "); } Else{string[] filelist=basedir.list ();  for(inti = 0; i < filelist.length; i++) {File ReadFile=NewFile (basedirname + "\ \" +Filelist[i]); //System.out.println (Readfile.getname ());                 if(!readfile.isdirectory ()) {Tempname=Readfile.getname (); if(Filesearcher.wildcardmatch (TargetFileName, tempname)) {//match succeeds, add file name to result setFilelist.add (Readfile.getabsolutefile ()); }                  } Else if(Readfile.isdirectory ()) {FindFiles (Basedirname+ "\\" +filelist[i],targetfilename,filelist); }              }          }      }            /*** Wildcard Match *@paramPattern Wildcard Mode *@paramstr string to match *@returnmatch succeeds returns true, otherwise false*/      Private Static BooleanWildcardmatch (string pattern, string str) {intPatternlength =pattern.length (); intStrlength =str.length (); intStrindex = 0; Charch;  for(intPatternindex = 0; Patternindex < Patternlength; patternindex++) {ch=Pattern.charat (Patternindex); if(ch = = ' * ') {                  //Wildcard asterisk * means that you can match any number of characters                 while(Strindex <strlength) {                      if(Wildcardmatch (pattern.substring (Patternindex + 1) , str.substring (strindex)) {return true; } strindex++; }              } Else if(ch = = '? ')) {                  //wildcard character question mark? means match any one of the charactersstrindex++; if(Strindex >strlength) {                      //indicates that no character matches have been made in Str.                     return false; }              } Else {                  if((strindex >= strlength) | | (ch! =Str.charat (strindex))) {                      return false; } strindex++; }          }          return(Strindex = =strlength); }         Public Static voidMain (string[] paramert) {//find files in this directoryString BaseDIR = "D:/file"; //looking for a file with an extension of txtString fileName = "*.txt"; List resultlist=NewArrayList ();           Filesearcher.findfiles (BaseDIR, FileName, resultlist); if(resultlist.size () = = 0) {System.out.println ("No File fount."); } Else {               for(inti = 0; I < resultlist.size (); i++) {System.out.println (Resultlist.get (i));//Displays the search results.             }          }      }  }  

This article transferred from: http://tiwson.iteye.com/blog/681888

Java recursively searches for matching files under a specified folder

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.