java线程池 多线程搜索文件包含关键字所在的文件路径

文件读取和操作类

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Test3 {

    private String keyWords;
    
    public Test3(String keyWords){
        this.keyWords = keyWords;
    }
    
    //计算文件数量
    public static int count = 0;
    
    //搜索后查询到的文件路径汇总文件地址
    public static String searchedFilePath = "G:/Document/HongDaXingYe/SearchedDir/searchedFile.txt";
    
    public static File searchedFile = null;
    
    public static FileOutputStream fos = null;
    
    public void search(){
        String path = "G:/Document/HongDaXingYe/Project/oa/workflow/home/weaver/ecology/workflow/";
        File file = new File(path);
        File[] files = file.listFiles();
        getFiles(files);
        System.out.println("count:" + count);
    }
    
    //递归搜索文件并写入搜索到的路径到文件
    public void getFiles(File[] files){
        FileInputStream fis = null;
        try{
            for(File file : files){
                count++;
                if(file.isDirectory()){
                    getFiles(file.listFiles());
                }else{
                    StringBuffer sb = new StringBuffer();
                    byte[] bytes = new byte[1024];
                    fis = new FileInputStream(file);
                    int len = 0;
                    while((len = fis.read(bytes)) != -1){
                        sb.append(new String(bytes, 0, len));
                    }
                    fis.close();
                    if(sb.indexOf(keyWords) >= 0){
                        System.out.println("包含关键字(" + keyWords + ")的文件路径:" + file.getAbsolutePath());
                        fos.write(("包含关键字(" + keyWords + ")的文件路径:" + file.getAbsolutePath() + System.lineSeparator()).getBytes());
                        fos.flush();
                    }
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
}

多线程处理类

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TestThread2 implements Runnable{

    private int taskNum;
    private String keyWords;
    
    public TestThread2(int taskNum, String keyWords){
        this.taskNum = taskNum;
        this.keyWords = keyWords;
    }
    
    public static void main(String[] args){
        String searchedFilePath = "G:/Document/HongDaXingYe/SearchedDir/searchedFile.txt";
        File searchedFile = new File(searchedFilePath);
        FileOutputStream fos = null;
        try{
            fos = new FileOutputStream(searchedFile);
            if(!searchedFile.exists()){
                searchedFile.createNewFile();
            }
            Test3.fos = fos;
        }catch(Exception e){
            e.printStackTrace();
        }
        Scanner input = new Scanner(System.in);
        System.out.println("请输入需要搜索的关键字(如果有多个,用英文半角逗号隔开):");
        String keyWordsStr = input.next();
        String[] keyWordsArr = null;
        if(keyWordsStr.indexOf(",") > 0){
            keyWordsArr = keyWordsStr.split(",");
        }else{
            keyWordsArr = new String[]{ keyWordsStr }; 
        }
        input.close();
        LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 100, 3600, TimeUnit.SECONDS, workQueue);
        for(int i = 0; i < keyWordsArr.length; i++){
            String keyWords = keyWordsArr[i];
            TestThread testThread = new TestThread(i, keyWords);
            executor.execute(testThread);
        }
        if(executor.isTerminated() && Test3.fos != null){
            try {
                Test3.fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    @Override
    public void run() {
        System.out.println("正在执行task " + taskNum);
        System.out.println("当前关键字:" + keyWords);
        Test3 test3 = new Test3(keyWords);
        test3.search();
        System.out.println("task " + taskNum + "执行完毕");
    }
    
}

 

posted @ 2018-08-06 16:40  ラピスラズリ(Dawn)  阅读(658)  评论(0编辑  收藏  举报