import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.TreeMap;
public class Kao3 {
public static void main(String args[])throws Exception{
File file = new File("\"C:\\Users\\ssss\\Desktop\\Hello World.txt\"");
display(file);
}
public static void display(File file)throws Exception{
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
TreeMap<String,Integer> tm = new TreeMap<String,Integer>();
while((line=br.readLine())!=null){
line.toLowerCase();
String reg1 = "\\s+";
String reg2 ="^[a-zA-Z]\\w*";
String str[] = line.split(reg1);
for(String s: str){
if(s.matches(reg2)){
if(!tm.containsKey(s)){
tm.put(s,1);
}
else{
tm.put(s,tm.get(s)+1);
}
}
}
}
System.out.println(tm);
}
}
~~~
编程实现统计一个文本文件中,字母出现的次数或单词出现的次数
最新推荐文章于 2024-08-04 17:16:44 发布