package demo;
import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author yeqv
* @program A2
* @Classname A3
* @Date 2022/1/19 20:55
* @Email w16638771062@163.com
*/
public class A3 {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
List<String> list = new ArrayList<>();
File file = new File("D:\\jdk.txt");
Pattern pattern = Pattern.compile("\s[a-zA-Z]+\s");
String len;
try {
BufferedReader buff = new BufferedReader(new FileReader(file));
while ((len = buff.readLine()) != null) {
Matcher matcher = pattern.matcher(len);
while (matcher.find()) {
set.add(matcher.group());
list.add(matcher.group());
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
int b = 1;
for (String s : set) {
int a = 0;
for (String s1 : list) {
if (s.equals(s1)) {
a += 1;
}
}
System.out.printf("%d单词:%s 数量:%d%n", b, s, a);
b += 1;
}
}
}
JAVA统计文件中每个单词出现的数量
原创
©著作权归作者所有:来自51CTO博客作者桃花键神的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
java怎么统计每个项目下的每个类别的数据
本文简要介绍了Java统计每个项目下的每个类别的数据的方法,给出了两个详细的代码示例。
Java System HashMap ArrayList -
Python 统计字符串中每个单词出现的次数
统计字符串中的每个单词的个数假定给定一 段字符串,不包括特殊字符,比如,,。等
python 列表 字符串 迭代 特殊字符 -
java按单词出现次数统计单词
import java.util.*;public class WordOccurrence implements Comparable { private String word; private int coun
java j2se Collection TreeMap i++