Java Learning (4): Statistics of a file in English, Chinese, numbers, other characters and the total number of characters

Source: Internet
Author: User

Requirements: Statistics of a file in English, Chinese, numbers, other characters and the total number of characters (this essay takes TXT file as an example)

Import Java.io.bufferedreader;import Java.io.file;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.ioexception;import java.io.inputstreamreader;/** * will be a file in English, Chinese, digital, Count the other characters and the total number of characters * * @author XCX * @time June 24, 2017 pm 4:12:53 */public class Statistics {public static void main (string[] Ar GS) throws IOException {string E1 = "[\u4e00-\u9fa5]";//chinese string E2 = "[A-za-z]";//english string E3 = "[0-9]";//Digital String fil E = "D:\\java\\dd.txt";//file path to read//create read-in byte stream fileinputstream fis = new FileInputStream (file);// Converts bytes into a character stream InputStreamReader ISR = new InputStreamReader (FIS);//Convert to cache mode BufferedReader br = new BufferedReader (ISR); int Numsum = 0;//record numeric character int letsum = 0;//record english character int punsum = 0;//record punctuation character int chinesesum = 0;//record literal character int totle = 0;//record total characters//Will The characters read out are copied to ssstring ss = ""; String S;while ((s = br.readline ()) = null) {SS + = s;} Traverse string temp;for (int i = 0; i < ss.length (); i++) {temp = string.valueof (Ss.charat (i)); if (Temp.matches (E1)) {/ /If the character matches the Chinese chinesesum++;} else if (temp.matches (E2)) {//If the character matches the English letsum++;} else if (Temp.matches (E3)) {///If the character matches the number numsum++;} elsepunsum++;//Other} Total number of characters Totle = numsum + letsum + punsum + chinesesum;//output System.out.println ("number:" + numsum + "x"); System.out.println ("English:" + letsum + "one"); System.out.println ("Other characters are:" + punsum + "one"); System.out.println ("Chinese:" + chinesesum + "one"); SYSTEM.OUT.PRINTLN ("Total characters are:" + Totle + "one");}}

  

Java Learning (4): Statistics of a file in English, Chinese, numbers, other characters and the total number of characters

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.