用户从属性文件中读取文件夹,指定关键字,统计出指定文件夹下面指定的关键字中,出现次数最多的关键字

用户从属性文件中读取文件夹,指定关键字,统计出指定文件夹下面指定的关键字中,出现次数最多的关键字

/这里只考虑了一个文件夹/

大体思路

1.从配置文件中读取路径和关键字字符串
2.分割关键字字符串
3.读取每一个文件,遍历每一个文件(查找关键字)
4.比较关键字出现的次数

读配置文件的类

package keyworldsfind.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

public class propertiesUtil {
	
	public static String propertiesread(String pathname,String Keyname) {
		Properties pps=new Properties();
		try (BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream (new File(pathname))));){
			
			pps.load(br);
			System.out.println(pps.getProperty(Keyname));
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		return pps.getProperty(Keyname);
	}
}

读文件的类,并对每一个文件的每一行进行统计

package keyworldsfind.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class fileReadUtil {
	
	public static void fileRead(File filePath,String[] keywordsArr,int[] keywordsCount) {
		if(filePath==null) {
			System.out.println("文件路径错误");
			return;
		}

		try(BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));){
			String line="";
			while((line=br.readLine())!=null) {
				//System.out.println(line);
				//统计每个文件中每一行关键字出现的次数,不分大小写
				keywordsCountByLine(line.trim().toLowerCase(),keywordsArr,keywordsCount);	
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
     }
	
	//统计每个文件中每一行关键字出现的次数
	public static void keywordsCountByLine(String line,String[] keywordsArr,int[] keywordsCount){
		
		if(line == null || keywordsArr.length == 0 || keywordsCount.length == 0){
			return;
		}
		
		int size=keywordsArr.length;
		for(int index=0;index<size;index++){
			
			 keywordsCount[index]+=AkeywordCountByLine(line,keywordsArr[index]);
		}
				
	}
	
	//正则表达式 统计每一行每个关键字的次数
	public static int AkeywordCountByLine(String line,String keyword){
		
		Pattern pattern=Pattern.compile(keyword);			
		Matcher matcher=pattern.matcher(line);
		int count=0;
		while(matcher.find()){
			count++;
		}
		return count;
	}
		
}
	


查找关键字的方法

package keywordsfind.service;

import java.io.File;

import keyworldsfind.util.fileReadUtil;

public class KeywordsService {
	
	//查找出现次数最多的关键字
	public static void keywordsMaxCount(String foldPath,String keywordsStr){
		
		if(foldPath == null){
			System.out.println("文件夹路径错误");
			return;
		}
		File fold=new File(foldPath);
		//保证是个目录且存在
		if(!fold.exists() || !fold.isDirectory()){
			System.out.println("此文件夹不存在");
			return;
		}

		//获取每个关键字
		String[] keywordsArr=keywordsStr.split(",");
		if(keywordsArr.length == 0){
			System.out.println("分割关键字发生错误");
			return;
		}
		int SIZE=keywordsArr.length;
		int[] keywordsCount=new int[SIZE];
		
		//统计每个关键字出现的次数
		keywordCount(fold,keywordsArr,keywordsCount);
		
		//查找出现次数最多的关键字
		int maxCount=keywordsCount[0];
		int resIndex=0;
		for(int index=1;index<SIZE;index++){
			if(maxCount<keywordsCount[index]){
				maxCount=keywordsCount[index];
				resIndex=index;
			}
		}
		System.out.println(keywordsArr[resIndex]+"出现的次数最多,是"+maxCount+"次");
	}
	
	//统计每个关键字出现的次数
	public static void keywordCount(File fold,String[] keywordsArr,int[] keywordsCount){
		
		if(!fold.exists() || !fold.isDirectory()){
			System.out.println("此文件夹不存在");
			return;
		}
		File[] filesPath=fold.listFiles();
		
		for(File filePath: filesPath){
			//列出目录中的全部内容
			//System.out.println(filePath);
			//统计每个文件中的关键字出现的次数
			keywordCountByFile(filePath,keywordsArr,keywordsCount);
		}
	}
	
	//统计每个文件中的关键字出现的次数
	public static void keywordCountByFile(File filePath,String[] keywordsArr,int[] keywordsCount){
		if(filePath == null){
			System.out.println("文件路径错误");
			return;
		}
		//读每个文件
		fileReadUtil.fileRead(filePath,keywordsArr,keywordsCount);
	}

}

主函数测试

package keywordsfind.service;

import java.io.FileNotFoundException;
import java.util.Scanner;

import keyworldsfind.util.propertiesUtil;

public class KeywordsTest {

	public static void main(String[] args) throws FileNotFoundException  {

		System.out.println("请输入配置文件的路径(注意路径的输入格式)");
		//D:\\workspace\\keyworldsfind\\src\\keyworldsfind\\util\\1.properties
		try(Scanner scanner=new Scanner(System.in);){
			String propertiesPath=scanner.next();
			if(propertiesPath == null){
				System.out.println("配置文件路径错误");
				return;
			}
			
			//获取文件夹的路径
			String foldPath=propertiesUtil.propertiesread(propertiesPath, "foldPath");
			//获取关键字的字符串
			String keywordsStr=propertiesUtil.propertiesread(propertiesPath, "keywordsStr");
			if(keywordsStr.length() == 0){
				System.out.println("没有需要查询的关键字");
				return;
			}
			
			//查找出现关键字次数最多的
			KeywordsService.keywordsMaxCount(foldPath, keywordsStr);
		}
	}
}

配置文件:在这里插入图片描述

运行结果

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值