public class Util{
public static void main(String[] args) throws IOException {
//键盘录入指定文件名
Scanner sc = new Scanner(System.in);
String fileName = sc.nextLine();
//调用方法,求结果
countWord(fileName);
}
public static void countWord(File filename) throws IOException {
Util util = new Util();
String[] str = util.readFile(filename);
Map<String,Integer> map = new HashMap<String,Integer>();
//遍历数组(即整篇文章)
for(String word : str){
if(map.containsKey(word)){
//包含该单词则加1
map.put(word, map.get(word)+1);
}else{
//第一次出现则为1
map.put(word, 1);
}
}
util.printCount(map);
}
public String[] readFile(File filename) throws IOException {
Reader reader = new FileReader(filename);
//BufferedReader可以读取行,读取速度快
BufferedReader br = new BufferedReader(reader);
//用于将读取的每一句拼接,去除边缘截断单词
StringBuffer sbu = new StringBuffer();
String line = "";
while (null != (line = br.readLine())) {
//拼接行
sbu.append(line);
}
// System.out.println(sbu);
String s = sbu.toString();
//正则中的非英文字符分割
String[] str = s.split("\\W");
return str;
}
//打印方法
public void printCount(Map map) {
Set<String> set = map.keySet();
for (String key : set) {
String str = key + "——>" + map.get(key);
System.out.println(str);
}
}
}