下列程序的目的是通过命令行参数给定一个文本文件,然后统计文本文件的字符数和行数并在标准输出上显示。请完成方法countChars(),使程序完整。(10分)

发布时间:2020-07-31 03:24:45

问题补充:

 

import java.io.*;

public class Count {

 

    //method countChars

//…………………….

 

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

    {

        if (args.length >= 1)

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

        else

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

    }

}


网友回答

public static void countChars(FileReader in)

  throws IOException { int charC = 0;

  int lineC = 0;

  String line; BufferedReader bf = new BufferedReader(in); while ((line=bf.readLine()) != null) { charC += line.length(); lineC++; }

  System.out.println("Counted " + charC + " chars "+"and "+lineC+" lines.");

  }
以上问题属网友观点,不代表本站立场,仅供参考!