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