首页 > 代码库 > 统计文本文件的字符数、单词数和行数

统计文本文件的字符数、单词数和行数

public class Test {

public static void main(String[] args) throws Exception{
Scanner input=new Scanner(System.in);
System.out.println("请输入路径");
String path=input.next();
int charNum= 0 ;
int wordsNum= 0;
int lineNum = 0;
InputStreamReader isr = new InputStreamReader(new FileInputStream(path)); 
BufferedReader br = new BufferedReader(isr);
while( br.read()!= -1){
String s = br.readLine();
charNum+=s.length();
wordsNum +=s.split(" ").length;
lineNum ++; 
}
isr.close();//关闭
System.out.println("字符数:"+charNum+"\t单词数:"+wordsNum+"行 数:"+lineNum); 

}
}

统计文本文件的字符数、单词数和行数