多线程同步查找包含特定内容的文件
import java.io.*; public class FileTest { public static void main(String[] args){ findFile("E:\\dubbo"); } public static void findFile(String path){ //获得目录下的文件列表 File[] fileList = new File(path).listFiles(); //目录下不为空 if(fileList.length > 0){ //遍历文件数组,如果是目录,递归调用本方法,如果是文件,判断是不是.java结尾,然后调用读文件方法判断 for(File file : fileList){ if(file.isDirectory()){ findFile(file.getAbsolutePath()); }else{ if(file.getName().endsWith(".java")){ threadTest thread = new threadTest(file,"log"); new Thread(thread).start(); } } } } } } class threadTest implements Runnable{ private File file; private String key; public threadTest(File file,String key) { this.file = file; this.key = key; } @Override public void run() { BufferedReader bufferedReader; try { //获取文件的读取流 bufferedReader = new BufferedReader(new FileReader(file)); StringBuffer sb = new StringBuffer(); String line = ""; //读取文件并追加到StringBuffer上 while((line = bufferedReader.readLine()) != null){ sb.append(line); } //匹配是否存在特定的关键字 if(sb.toString().contains(key)){ System.out.println(file.getName()); } } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } } }