Java查找指定文件中指定字符的个数

时间:2022-10-26 19:48:50
 package lwl.youweb2.test;

 import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern; /**
* 查找指定文件中指定字符的个数
*
* @author liuwenlong
* @create 2020-08-20 10:48:27
*/
@SuppressWarnings("all")
public class test01 {
static {
String args[] = {"d:\\123.txt", "张三"};
main(args);
} public static void main(String[] args) {
if (args.length != 2) {
return;
}
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(args[0]);
br = new BufferedReader(fr);
int cnt = 0;
String str = null;
while ((str = br.readLine()) != null) {
Pattern a = Pattern.compile(args[1]);
Matcher m = a.matcher(str);
while (m.find()) {
cnt++;
}
}
System.out.println("[" + args[1] + "]的数量:" + cnt);
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }

Java查找指定文件中指定字符的个数

Java查找指定文件中指定字符的个数