关键字查找Util

此工具类主要用于:遍历指定文件夹下所有文件是否包含 指定的关键字,将包含此关键字的文件 完整路径打印 并计数。

此工具类 可使用多关键字,且不区分文件夹下文件类型

应用场景:查找公司 指定目录下配置信息,查找xxx.js中什么方法 在整个项目中 被调用的次数 

Code:

package com.tao.mapper;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class SearchStrInPath {
	 public static int mount = 0; 
	 public static void main(String[] args) {
		 String filename = "E:\\gateway-config\\zc-autoconfig-sh";
		 //创建一个 File 实例,表示路径名是指定路径参数的文件  
	     File file = new File(filename);
	     args=new String[]{"webgateMailHost","mailUserName","mailPassword"};
	     for(int i=0;i<args.length;i++) {
	    	 findFile(file, args[i]); 
	    	 print(args[i]);
	     }

	}
	 
	 public static boolean isTrueFile(File file){  
		 if(!file.exists() || !file.canRead()){
			 return false;
		 }  
         if(file.getName().startsWith(".")){
        	 return false;
         } 
         if(file.getName().endsWith(".")){
        	 return false;
         }  
         return true;
	 }
	 
	    public static void findFile(File file, String word){  
	        File[] listFiles = file.listFiles();   
	        //得到一个File数组,它默认是按文件最后修改日期排序的  
	        for (int i = 0; i < listFiles.length; i++){  
	           if (listFiles[i].isDirectory())  
	           findFile(listFiles[i], word);  
	           else if (isTrueFile(listFiles[i]))  
	           search(listFiles[i], word);  
	        }  
	    }
	    
	    public static void search(File file, String word){  
	        try   
	        {  
	            int j = 0, k = 0, ch = 0;  
	            String str = null;  
	            FileReader in = new FileReader(file);  
	            while ((ch = in.read()) != -1)   
	            {  
	                str += (char) ch;  
	            }  
	            if (str != null)  
	            {  
	                while (str.indexOf(word, j) != -1)   
	                {  
	                    k++;  
	                    j = str.indexOf(word, j) + 1; // 返回第一次出现的指定子字符串在此字符串中的索引  
	                }  
	            }  
	            if (k > 0)   
	            {  
	                System.out.println("在" + file.getAbsolutePath() + "有    " + k+ " 个关键字" + word);  
	                mount++;  
	            }  
	            in.close();  
	        }   
	        catch (FileNotFoundException e)  
	        {  
	            e.printStackTrace();  
	        }   
	        catch (IOException e)  
	        {  
	            e.printStackTrace();  
	        }  
	    }
	    
	    public static void print(String word){  
	        if (mount != 0){  
	            System.out.println("一共找到    " + mount + " 个文件包含关键字" + word + "! \n");
	            mount=0;
	        }   
	        else{  
	            System.out.println("没有找到相应的文件");  
	        }  
	    } 

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值