package com.gennlife.epidemicdatamng.modules.zy.service.impl;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* 统计某字符串在文件中出现的次数
*
* @author ConstXiong
*/
public class CountString {
public static int count(String filename, String target)
throws FileNotFoundException, IOException {
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
StringBuilder strb = new StringBuilder();
while (true) {
String line = br.readLine();
if (line == null) {
break;
}
strb.append(line);
}
String result = strb.toString();
int count = 0;
int index = 0;
while (true) {
index = result.indexOf(target, index + 1);
if (index > 0) {
count++;
} else {
break;
}
}
br.close();
return count;
}
public static void main(String[] args) {
try {
int num = 1645894;
int log = 0;
for (int i = 0; i < 32; i++) {
num = num + count("/Users/yukaili/12/data 2/logs/gateway-server/2022-02/info.2022-02-06." + log + ".log", “123");
System.out.println(log + "次数,总统计量" + num);
log++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}