最近公司考核笔试,竟然做不出来,呀呀呀~真的缺了 IDE 就不会写代码了,还有就是一直面向搜索引擎编程(泪流满面 T_T),得端正一下了
public static void main(String[] args) {
try {
// 读取文件
BufferedReader br = Files.newBufferedReader(Paths.get("C:\\Users\\wongt\\Desktop\\1.txt"), Charset.forName("UTF-8"));
int count = 0;
// 使用正则来匹配字符
Pattern p = Pattern.compile("小白云");
while (true) {
String line = br.readLine();
// 读到文件末尾就退出循环
if (null == line) {
break;
}
Matcher m = p.matcher(line);
// 执行匹配并统计次数
while (m.find()) {
count++;
}
}
System.out.println(count);
} catch (IOException e) {
e.printStackTrace();
}
}