Java read local English txt text, showing the number of lines, words, words appear frequency

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.