思路:逐行读,逐个字母判断,用Map统计。
import java.io.*; import java.util.*; class StringCountOfFile{ public static void main(String argv[])throws Exception{ File fileSrc=new File("StringCountOfFile.java"); if(!fileSrc.exists()){ throw new Exception("目录不存在"); } BufferedReader fin=new BufferedReader( new FileReader(fileSrc) ); String line; Map counter= new HashMap(); while( (line=fin.readLine())!=null){ int len = line.length(); for(int i=0; i='0' && c<='9') || (c>='A' && c<='Z') || (c>='a' && c<='z') ) ){ continue; } if(counter.containsKey(c)){ counter.put(c, counter.get(c)+1); }else{ counter.put(c, 1); } } } fin.close(); for(Iterator it=counter.keySet().iterator(); it.hasNext(); ){ char key=it.next(); int count=counter.get(key); System.out.println(key+" --- "+count); } } }