集册 Java实例教程 使用InputStream对文件中的char进行计数

使用InputStream对文件中的char进行计数

欢马劈雪     最近更新时间:2020-01-02 10:19:05

389
使用InputStream对文件中的char进行计数
// 来自 n  o  w  j  a  v  a . c o m

import java.io.*;


public class Count {

    public static void countChars(InputStream in) throws IOException {

        int count = 0;


        while (in.read() != -1)

            count++;


        System.out.println("Counted " + count + " chars.");

    }


    public static void main(String[] args) throws Exception {

        if (args.length >= 1)

            countChars(new FileInputStream(args[0]));

        else

            System.err.println("Usage: Count filename");

    }

}/*from N o w  J a v a  . c o m*/


展开阅读全文