Java新手在这里!
我正在编写一个程序来练习读取输入并将输出写入文件。我已经完成了程序的编码,但是当我运行它时,程序只是捕获并继续执行 FileNotFoundException。
该文件位于程序的源文件夹中,我什至尝试将其放在与程序相关的每个文件夹中。我试过了:
- 在方法头中声明异常
- 用 try/catch 块包围有问题的部分。
- 以上两者结合。
这是导致问题的相关代码。有什么突出我想念的吗?
public static void main(String[] args) throws FileNotFoundException {
Scanner keyboard = new Scanner(System.in);
String playerHighestScore = "", playerLowestScore = "";
int numPlayers = 0, scoreHighest = 0, scoreLowest = 0;
System.out.println("Enter an input file name: ");
String inputFileName = keyboard.nextLine();
String outputFileName = getOutputFileName(keyboard, inputFileName);
File inputFile = new File(inputFileName);
try {
Scanner reader = new Scanner(inputFile);
reader.close();
}
catch (FileNotFoundException exception) {
System.out.println("There was a problem reading from the file.");
System.exit(0);
}
Scanner reader = new Scanner(inputFile);
PrintWriter writer = new PrintWriter(outputFileName);