Using java to write code, the statistics of the number of each word txt file, output in the console

static void Run the display public () throws Exception {
File File = new new File ( "D: \ to Review of 01 \ summer job folder \ shujiawork07_1 \ src \ COM \ czxy \ Domain01 \ wordcount.txt");
BufferedReader br = new new BufferedReader ( the FileReader new new (file));
String Line = null;
// map defines a number of words and a set of stored word appears
the TreeMap <String, Integer> (TM) = new new the TreeMap <String, Integer> ();
// read the file
while ((line = br.readLine ()) ! = null) {

    line.toLowerCase();
    String reg1 = "\\s+";
    String reg2 ="\\w+";
    //将读取的文本进行分割
    String str[] = line.split(reg1);
    for(String s: str){
        if(s.matches(reg2)){
            //判断集合中是否已经存在该单词,如果存在则个数加一,否则将单词添加到        //集合中,且个数置为1
            if(!tm.containsKey(s)){
                tm.put(s,1);
            }else{
                tm.put(s,tm.get(s)+1);
            }
        }
    }
}
System.out.println(tm);

}

Published 56 original articles · won praise 561 · views 20000 +

Guess you like

Origin blog.csdn.net/CZXY18ji/article/details/101513430