62,614
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) throws IOException {
RandomAccessFile randomAccessFile = new RandomAccessFile("aaaa.txt", "r");
Map<String,Integer> map = new HashMap<String, Integer>();
Pattern pattern = Pattern.compile("\\b[a-zA-z]+?\\b");
String str = null;
while ((str = randomAccessFile.readLine()) != null) {
Matcher matcher = pattern.matcher(str);
while(matcher.find()){
String s = matcher.group();
int count = 1;
if(map.get(s) != null)
count += map.get(s);
map.put(s, count);
}
}
System.out.println(map.size());
for(Entry<String,Integer> entry : map.entrySet()){
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}