欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java文件操作代码片断实例实现统计文件中字母出现的个数功能

程序员文章站 2024-02-21 13:32:04
复制代码 代码如下:string filename = "d:/date.java.bak";      &nb...

复制代码 代码如下:

string filename = "d:/date.java.bak";
        // string filename = "d:/test.qqq";
        string line;
        int i = 0, j = 0, f = 0, k = 0;
        try {
            bufferedreader in = new bufferedreader(new filereader(filename));
            line = in.readline();
            while (line != null) {
                // system.out.println(line);
                char c[] = line.tochararray();
                for (int i1 = 0; i1 < c.length; i1++) {
                    // 如果是字母
                    if (character.isletter(c[i1]))
                        i++;
                    // 如果是数字
                    else if (character.isdigit(c[i1]))
                        j++;
                    // 是空格
                    else if (character.iswhitespace(c[i1]))
                        f++;
                }
                line = in.readline();
                k++;
            }
            in.close();
            system.out
                    .println("字母:" + i + ",数字:" + j + ",空格:" + f + ",行数:" + k);
        } catch (ioexception e) {
            e.printstacktrace();
        }