我需要帮助将JFileChooser实现到此程序中。对于这个问题,我想制作将读取文件并显示该文件中的字符,单词和行数的代码。我的教授评论了我的任务并说“仍然找不到文件 - 使用FileChooser并重新提交。”感谢任何帮助,谢谢你提前。
import java.io.*;
import java.util.*;
public class C12E13 {
public static void main(String[] args) {
while (true) {
System.out.println("The program will count the number of characters, words, and lines in a file");
System.out.println("Input File Name");
Scanner input = new Scanner(System.in);
String filename = input.nextLine();
File source = new File(filename);
if (!source.exists()) {
System.out.println(filename + " File does not exist");
System.exit(2);
}
try {
Scanner infile = new Scanner(source);
String line;
int charactersCount = 0;
int wordsCount = 0;
int linesCount = 0;
while (infile.hasNextLine()) {
line = infile.nextLine();
linesCount++;
String[] words = line.split("");
wordsCount += words.length;
for (String token : words) {
charactersCount += token.length();
}
}
System.out.println("Name of the input file:" + filename);
System.out.println("Number of lines in the file:" + linesCount);
System.out.println("Number of words in the file:" + wordsCount);
System.out.println("Number of characters in the file:" + charactersCount);
infile.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Do you want input again Y/N");
Scanner scan = new Scanner(System.in);
String inp = scan.nextLine();
if (inp.equals("N")) {
break;
}
}
}
}
}