读取a.txt文件,算出里面有多少个字母和多少个大写字母

解决方案 »

  1.   

    大写字母和小写字母的ACsii码不一样,从这一点入手,不是很难。
      

  2.   

    可以用bufferreader来读取 每次比较Accii码或者就直接比较A-Z a-z
      

  3.   


    public static void main(String[] args) {
    BufferedReader r = null;
    int m = 0,n=0;
    try {
    r = new BufferedReader(new FileReader(new File("a.txt")));
    String s = null;
    StringBuffer ss=  new StringBuffer();
    while((s = r.readLine())!=null)
    ss.append(s);
    for(int i = 0; i<ss.length(); i++){
    char c = ss.charAt(i);
    if(c>='a'&& c<'z')
    m++;
    else if(c>='A'&& c<='Z')
    n++;
    }
    System.out.println("小写:"+m+",大写:"+n);
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    try {
    r.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
      

  4.   

        private final static void counting(String fileName) {
            try {
                InputStreamReader reader = new InputStreamReader(new FileInputStream(fileName));
                char[] buffer = new char[1024];
                int letters = 0;
                int cLetters = 0;
                while (reader.read(buffer, 0, 1024) != -1) {
                    for (char c : buffer) {
                        if ((c >= 65 && c <= 91)
                            || (c >= 97 && c <= 123)) {
                            letters++;
                            if (c >= 97 && c <= 123) {
                                cLetters++;
                            }
                        }
                    }
                }
                System.out.println("所有字母:" + letters);
                System.out.println("大写字母:" + cLetters);
            } catch (FileNotFoundException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }