1. 程式人生 > >多線程同步查找包含特定內容的文件

多線程同步查找包含特定內容的文件

args while 是不是 lis 匹配 ringbuf sta print filelist

package com.zdst.scs.business.hystric.checkflow;

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(); } } }

多線程同步查找包含特定內容的文件