Create a new program that: •Counts the number of lines and the number of words in a text file; and •Prints the number of lines and the number of words to the terminal. 计算文本文件中的行数和单词数;然后输出到终端 import java.util.Scanner; import java.io.File; public class PartB3 { //计算文本文件中的行数和单词数;然后输出到终端 public static void main(String[] args) throws Exception { //存放文本文件中的行数,和单词数。 int amountOfLine = 0, amountOfWords = 0; /** * 读取文件 */ Scanner file = new Scanner(new File("amount.txt")); while(file.hasNextLine()) { amountOfLine ++; file.nextLine(); } //关闭扫描器重新扫描 file.close(); file = new Scanner(new File("amount.txt")); while(file.hasNext()) { amountOfWords ++; file.next(); } System.out.println("此文件共" + amountOfLine + "行"); System.out.println("此文件共" + amountOfWords + "词"); } }