统计一个文件中各个字母出现次数

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

/** 
* @author  万星明
* @version 创建时间:2018年10月19日 下午4:32:00 
* 3、统计一个文件xxxx.txt中各个字母出现次数:
* 	   例如(A(8),B(16),C(10)...,a(12),b(10),c(3)....,括号内代表字符出现次数; 

*/
public class Test3 {

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("c.txt")));
		int[] count  = new int[26];
		int[] COUNT  = new int[26];
	
		char[] c = new char[1];
		int len = br.read(c);
		while(len!=-1) {
			
			if(c[0]<='Z'&&c[0]>='A') {
				int number = c[0];
				COUNT[number-65]++;
			}
			if(c[0]<='z'&&c[0]>='a') {
				int number = c[0];
				count[number-97]++;
			}
			len = br.read(c);
		}
		Print(count, COUNT);
		System.out.print("\nb.txt文件读取完毕!");
		br.close();
		
	}
	public static void Print(int[] count,int[] COUNT) {
		for(int i=0;i<26;i++) {
			if(count[i]>0) {
				char lowerCase = (char)(i+97);
				System.out.println(lowerCase+"("+count[i]+")");
			}
			if(COUNT[i]>0) {
				char upperCase = (char)(i+65);
				System.out.println(upperCase+"("+COUNT[i]+")");
			} 
		}
		
	}


}

你可能感兴趣的:(java基础)