java实现搜索文件夹中所有文件包含的关键字的文件路径,递归搜索

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

public class Test {
    
    //计算文件数量
    public static int count = 0;
    
    //关键字
    public static String keyWords = "人员姓名";
    
    //搜索后查询到的文件路径汇总文件地址
    public static String searchedFilePath = "G:/Document/HongDaXingYe/SearchedDir/searchedFile.txt";
    
    public static File searchedFile = new File(searchedFilePath);
    
    public static FileOutputStream fos = null;
    
    public static void main(String[] args){
        String path = "G:/Document/HongDaXingYe/Project/oa/workflow/home/weaver/ecology/workflow/";
//        String path = "G:/Document/HongDaXingYe/Project/oa/workflow/home/weaver/ecology/workflow/mode/";
        try{
            fos = new FileOutputStream(searchedFile);
            if(!searchedFile.exists()){
                searchedFile.createNewFile();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        File file = new File(path);
        File[] files = file.listFiles();
        getFiles(files);
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(count);
    }
    
    //递归搜索文件
    public static void getFiles(File[] files){
        FileInputStream fis = null;
        try{
            for(File file : files){
                count++;
                if(file.isDirectory()){
                    getFiles(file.listFiles());
                }else{
                    StringBuilder sb = new StringBuilder();
                    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(file.getAbsolutePath());
                        fos.write((file.getAbsolutePath() + System.lineSeparator()).getBytes());
                        fos.flush();
                    }
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    
}