日期:2014-05-20 浏览次数:20790 次
import java.io.*; public class Counter { public static void main(String[] args) throws Exception { InputStream in = new FileInputStream(new File("out.txt")); BufferedReader reader = new BufferedReader( new InputStreamReader(in)); String line; int[] counters = new int[10]; int lineCounter = 0; while( (line = reader.readLine()) != null ) { lineCounter++; count(line, counters); } System.out.println("文字出现了" + lineCounter + "行"); for(int i=0; i<counters.length; i++) { System.out.printf("%d出现%d次\n", i, counters[i]); } reader.close(); in.close(); } private static void count(String line, int[] counters) { for(int i=0; i<line.length(); i++) { char c = line.charAt(i); if( Character.isDigit(c) ) counters[c - '0']++; } } }