我们简单的写一个测试程序,从test.txt中读取文件内容,记录test.txt中的行数和单词个数
第一个代码,我们使用了两个try语句,分别使用scanner来进行读取,结果是正确的。
public static void main(String[] args) {
File file = new File("src//test.txt");
try (Scanner scanner = new Scanner(file)){
int lines = 0;
while (scanner.hasNext()){
String str = scanner.nextLine