PackageCom.zdst.scs.business.hystric.checkflow;ImportJava.io.*; Public classFiletest { Public Static voidMain (string[] args) {FindFile ("E:\\dubbo"); } Public Static voidfindfile (String path) {//get a list of files under the directoryfile[] FileList =NewFile (path). Listfiles (); //The directory is not empty if(Filelist.length > 0){ //iterate through the file array, if it is a directory, call this method recursively, if it is a file, determine whether the. Java end, and then call the Read file method to determine for(File file:filelist) {if(File.isdirectory ()) {FindFile (File.getabsolutepath ()); }Else{ if(File.getname (). EndsWith (". Java") {threadtest thread=NewThreadTest (file, "log"); Newthread (thread). Start (); } } } } }} classThreadTestImplementsrunnable{Privatefile file; PrivateString Key; Publicthreadtest (File file,string key) { This. File =file; This. Key =key; } @Override Public voidrun () {BufferedReader bufferedreader; Try { //gets the read stream of the fileBufferedReader =NewBufferedReader (Newfilereader (file)); StringBuffer SB=NewStringBuffer (); String Line= ""; //read the file and append it to the StringBuffer while(line = Bufferedreader.readline ())! =NULL) {sb.append (line); } //matches whether a specific keyword exists if(Sb.tostring (). Contains (key)) {System.out.println (File.getname ()); } } Catch(FileNotFoundException e) {e.printstacktrace (); }Catch(IOException e) {e.printstacktrace (); } } }
Multi-threaded synchronization to find files that contain specific content