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