/** * Title: Title: Enter a line of characters, respectively, the number of English letters, spaces, numbers and other characters. * Time: July 28, 2015 10:04:33 * file: Lianxi07.java * cutter_point */package Bishi.zuixin50.t2015728;import Java.io.bufferedreader;import Java.io.file;import Java.io.fileinputstream;import Java.io.FileOutputStream;import Java.io.inputstreamreader;import Java.util.arraylist;import Java.util.hashmap;import Java.util.Iterator;import Java.util.list;import Java.util.map;import Java.util.map.entry;public class Lianxi07 {public static void main (string[] args) {//statistics all English letters, i.e. ASCII code string path = "Source/zuixin50/lianxi07input.txt"; String path2 = "Source/zuixin50/lianxi07output.txt"; List lis = lianxi07.getlines (path);//Get all the characters in the text map map = Lianxi07.count (LIS);//bar statistic data output to file Lianxi07.out (map, path2);} public static void out (map map, String path) {//We traverse map, bar data output to file FileOutputStream fos = null;try {fos = new Fileoutputstrea M (new File);//Traverse Mapfor (Object o:map.entryset ()) {map.entry<string, integer> Entry = (entry<string, integer>) o;//In fact, you can lose directly here.Out: the number of out = Entry.getkey () + "is:" + entry.getvalue () + "\ n";//This writing does not have to be judged if (entry.getkey () = = "Zimu") {String out = "The number of letters is: "+ entry.getvalue () +" \ n "; System.out.println (out); Fos.write (Out.getbytes ());} else if (entry.getkey () = = "Kongge") {String out = "The number of spaces is:" + entry.getvalue () + "\ n"; System.out.println (out); Fos.write (Out.getbytes ());} else if (entry.getkey () = = "Shuzi") {String out = "Number of digits is:" + entry.getvalue () + "\ n"; System.out.println (out); Fos.write (Out.getbytes ());} Else{string out = "The other number is:" + entry.getvalue () + "\ n"; System.out.println (out); Fos.write (Out.getbytes ());}}} catch (Exception e) {e.printstacktrace ();} Finally{try {fos.close ();} catch (Exception E2) {e2.printstacktrace ();}}} /** * We count all the letters, spaces, numbers and other characters in this line of data * @param lines line String * @return */public static map count (List lines) {//Here we use a Map to store the corresponding number According to this, key is the attribute we want to count: letters, spaces ... Value is the number of values map Tongji = new hashmap<string, integer> ();//Initialize Maptongji.put ("Zimu", 0); Tongji.put ("Kongge", 0); Tongji.put ("Shuzi", 0); Tongji.put ("Other", 0);/others//First we traverse all rows iterator it = Lines.iterator (), while (It.hasnext ()) {//one row row of values char[] all = (String) it.next ()). ToCharArray ();//We traverse this character array for (int i = 0; i < all.length; ++i) {if ((All[i] >= ' A ' && all[i] <= ' z ') | | (All[i] >= ' A ' && all[i] <= ' Z ')) {int value = (int) tongji.get ("Zimu");//We +1tongji.put the data ("Zimu", ++value);} else if ((All[i] >= ' 0 ' && all[i] <= ' 9 ')) {//The number of our statistics int value = (int) tongji.get ("Shuzi");//We put the data +1tongji. Put ("Shuzi", ++value);} else if (all[i] = = ") {//If it is a space, int value = (int) tongji.get (" Kongge ");//We +1tongji.put the data (" Kongge ", ++value);} else{//other int value = (int) tongji.get ("other");//We +1tongji.put the data ("other", ++value);}} for}//while//statistics completed return Tongji;} public static string GetLine (string path) {//Gets a row of data from a file string line = null;//a file input stream InputStreamReader ISR = null;// Used to read a line bufferedreader br = null;try {ISR = new InputStreamReader (new FileInputStream (new File)); br = new Bufferedread ER (ISR); line = Br.readline ();} catch (EXception e) {//TODO auto-generated catch Blocke.printstacktrace ();} finally{//Close IO resource try {br.close (); Isr.close ();} catch (Exception e) {e.printstacktrace ()}} return line;} The contents of the read file are saved to the array in string public static list Getlines (string path) {//Gets a row of data from the file string line = null; list<string> lines = new ArrayList ();//a file input stream InputStreamReader ISR = null;//used to read a line bufferedreader br = null;try {is R = new InputStreamReader (new FileInputStream (path)); br = new BufferedReader (ISR); while (line = Br.readline ()) ! = NULL) {//bar reads a row of data into the array lines.add (line);}} catch (Exception e) {e.printstacktrace ();} Finally{try {//Close IO resource br.close (); Isr.close ();} catch (Exception e) {e.printstacktrace ();}} return lines;}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Written test" 7, statistics of the number of English letters, spaces, numbers and other characters