问题补充:
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.");
}
下列程序的目的是通过命令行参数给定一个文本文件 然后统计文本文件的字符数和行数并在标准输出上显示。请完成方法countChars() 使程序完整。(10分)