Ideas:
1. Store the file content in stringbuffer.
2. Use the split () function to split the string. You can press (",", ".", "!", "Space", "press ENTER") to split and obtain an array.
3. traverse the array and put it into a Map <string, integer>. Key = word, value = word appears.
4. Sort the map if the most frequently used words in the file are required.
Certificate -----------------------------------------------------------------------------------------------------------------------------------------
The following is the file where the main function of filewordcount. Java is located.
Import Java. io. bufferedreader; import Java. io. filenotfoundexception; import Java. io. filereader; import Java. io. ioexception; import Java. util. *;/*** created by intellij idea. * User: Fly * Date: 11-9-13 * Time: * to change this template use file | Settings | file templates. */public class filewordcount {public static void main (string [] ARGs) {try {bufferedreader BR = new bufferedreader (New filereader ("D: \ test.txt"); string s; stringbuffer sb = new stringbuffer (); While (S = Br. Readline ())! = NULL) {sb. append (s) ;}map <string, integer> map = new hashmap <string, integer> (); stringtokenizer ST = new stringtokenizer (sb. tostring (),",.! \ N "); While (St. hasmoretokens () {string letter = ST. nexttoken (); int count; If (map. get (letter) = NULL) {COUNT = 1;} else {COUNT = map. get (letter ). intvalue () + 1;} map. put (letter, count) ;}set <wordentity> set = new treeset <wordentity> (); For (string key: map. keyset () {set. add (New wordentity (Key, map. get (key);} // concatenate a string by yourself and output the desired string format: system. out. println ("output form 1:"); For (iterator <wordenti Ty> it = set. iterator (); it. hasnext ();) {wordentity W = it. next (); system. out. println ("word:" + W. getkey () + "appears at:" + W. getcount ();} // print the wordentity object directly to achieve the desired output. You only need to rewrite the tostring () method system in the wordentity class. out. println ("output form 2:"); For (iterator <wordentity> it = set. iterator (); it. hasnext ();) {wordentity W = it. next (); system. out. println (w);} // we can control the output of only the top three to system. out. println ("output form 3:"); int Count = 1; for (iterator <wordentity> it = set. iterator (); it. hasnext ();) {wordentity W = it. next (); system. out. println ("Number" + Count + "name:" + W. getkey () + "appears at:" + W. getcount (); If (COUNT = 3) // when the output is 3, the loop break pops up; count ++ ;}} catch (filenotfoundexception e) {system. out. println ("file not found ~! ");} Catch (ioexception e) {system. Out. println (" File Read exception ~! ") ;}} Wordentity. java file/*** created by intellij idea. * User: Fly * Date: 11-9-13 * Time: * to change this template use file | Settings | file templates. */public class wordentity implements comparable <wordentity> {private string key; private integer count; Public wordentity (string key, integer count) {This. key = key; this. count = count;} public int compareto (wordentity O) {int C MP = count. intvalue ()-O. Count. intvalue (); Return (CMP = 0? Key. compareto (O. key):-CMP); // you only need to add a negative number here to determine whether the order is ascending or descending-CMP descending, CMP sort in ascending order // because the treeset will call the compareto method of workformap to determine its own sorting} @ override Public String tostring () {return key + "appears for the following times: "+ count;} Public String getkey () {return key;} public integer getcount () {return count ;}} output result: output form 1: word: is appears for: 4 words: My appears for: 2 words: Very appears for: 2 words: word appears for: 2 words: the number of times a appears: 1 word: are: 1 word: boy appears: 1 word: English appears: 1 word: fool appears at: 1 word: Good appears at: 1 word: Hah appears at: 1 word: Hello appears at: 1 word: hi: 1 word: I: 1 word: Love: 1 word: Mary: 1 word: the number of times a much appears: 1 word: Name: 1 word: OK: 1 word: Poor: 1 word: so appears: 1 word: Sophie appears: 1 word: You appear: 1 Output Form 2: Is appears: 4. The number of occurrences of my is: 2. The number of occurrences of very is: 2. The number of occurrences of word is: 2. The number of occurrences of A is: 1. The number of occurrences of a boy is: 1. The number of occurrences of a good English is: 1. The number of occurrences of a good English is: 1. The number of occurrences of a hah is: 1. The number of Hello appears is: 1. The number of Hi appears is: 1. The number of I appears is: 1. The number of love appears is: 1. The number of mary appears is: 1 much: 1 Name: 1 OK: 1 poor: 1 so: 1. The number of times that Sophie appears is: 1. the number of times that you appear is: 1. The output format is "3". The number of times that 1st words are: 4. The number of times that 2nd words are: 2 3rd words: Very appears for a number of times: 2
Original article address
Method 2
// Countword. java package COM. xie. tencent; import Java. io. bufferedreader; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. ioexception; import Java. io. inputstreamreader; import Java. util. hashmap;/*** this class is used to count the number of times a word appears in all files. * @ Author center **/public class counterword {filereader fr; private hashmap <string, integer> hmap = new hashmap <string, integer> (); Public hashmap <string, integer> gethmap () {return hmap;}/*** this constructor is used to hold the opposite party's reference * @ author center * @ Param F filereader */Public counterword (filereader F) {Fr = f;}/*** open the file based on the input file path and analyze the number of words in the file, * store a hashmap * @ author center * @ Param path the absolute path of the string file * @ Re The return value of turn seems useless. You can receive */Public Boolean openfile (string path) {Boolean B = true when needed; try {fileinputstream FD = new fileinputstream (PATH ); bufferedreader BR = new bufferedreader (New inputstreamreader (ISI); try {string words = BR. readline (); While (words! = NULL) {// system. out. println (words); string [] Word = words. split (","); For (INT I = 0; I <word. length; I ++) {addwordcount (word [I]);} words = BR. readline () ;}} catch (ioexception e) {system. out. println ("An error occurred while reading the file. "); E. printstacktrace () ;}} catch (filenotfoundexception e) {system. out. println ("the file is not found:" + path); B = false; E. printstacktrace ();} return B;}/*** put the word into map, and count the number of times the word appears * @ Param word string word */private void addwordcount (string word) {If (hmap. containskey (Word) {hmap. put (word, hmap. get (word ). intvalue () + 1);} else {hmap. put (word, 1 );}}