Refer to the code on the Internet, and then make a slight change, making records purely
ImportJava.io.BufferedReader;ImportJava.io.FileReader;Importjava.io.IOException;ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.Map;ImportJava.util.Set; Public classReadFile { Public Static voidMain (string[] args) {intlinenum = 0;//define the number of variable rowsHashMap<String,Integer> map =NewHashmap<string,integer> ();//define a map to hold words and occurrences in textBufferedReader in =NULL; Try{ in=NewBufferedReader (NewFileReader ("F:\\ec.txt"));//Text AddressString buffer =NULL; // while(buffer = In.readline ())! =NULL) {LineNum++;
string[] Words= Buffer.replaceall ("[^a-za-z]+", "" "). Trim (). Split (" ");//Regular Expression Segmentation for(inti = 0; i < words.length; i++) {map.put (Words[i],map.getordefault (words[i),0) +1);//value default is 0, each repetition 1 times plus 1}}//This paragraph is the data Set saved in the output map<Map.Entry<String,Integer>> set =Map.entryset (); for(Iterator<map.entry<string,integer>> it =set.iterator (); It.hasnext ();) {Map.entry<String,Integer> entr =(Map.entry<String,Integer>) It.next (); System.out.println (Entr.getkey ()+"--------"+Entr.getvalue ()); } System.out.println (Number of rows +linenum); System.out.println ("Word count" +map.size ()); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } finally { Try{in.close (); Turn off buffered character streams
} Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } }}
Java reads local txt text, showing the number of rows, words, and occurrences of words