利用Scanner类读取file文件
第一步创建有个txt文件,名字命名为wordTest
hello word
jwq
zwq
zhn
第二步写一个main方法
public static void main(String[] args) {
File file = new File("D:\\****\\java读取文件\\wordTest.txt");
try{
Scanner sc= new Scanner(file);
int count=0;
int wordLine=0;
int line=0;
while (sc.hasNext()){
String a=sc.nextLine();
String b=a.replaceAll(" ", "");
line+=1;
count+=b.length();
String [] c=a.split(" ");
for (int i = 0; i < c.length; i++) {
wordLine+=1;
}
}
System.out.println("字符数:"+count);
System.out.println("单词数:"+wordLine);
System.out.println("行数:"+line);
}catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Scanner
Scanner sc=new Scanner();
sc..nextInt()