gpt4 book ai didi

java - 如果找不到文件,如何显示错误消息?

转载 作者:行者123 更新时间:2023-12-03 09:08:29 25 4
gpt4 key购买 nike

我编写了一个程序来读取特定文件,并且想知道如果找不到该文件该如何显示自定义消息。我目前无法使用的内容,有人可以解释为什么吗?

try {
//create the file writer
Fwrite = new FileWriter(file);
Fwrite.write("Student Name \t\t\t Test Score \t Grade\n");
for (int i = 0; i < size; i++) {
Fwrite.write(students[i].getStudentLName() + ", " + students[i].getStudentFName() +
" \t\t\t "+ students[i].getTestScore() + " \t\t " + students[i].getGrade() + "\n");
}
Fwrite.write("\n\nHighest Test Score: " + highestScore + "\n");

Fwrite.write("Students having the highest test score\n");

//writes the test scores in descending order
for (int i = 0; i < size; i++) {
if (students[i].getTestScore() == highestScore) {
Fwrite.write(students[i].getStudentLName() + ", ");
Fwrite.write(students[i].getStudentFName() + "\n");
}
}

//catches any errors
} catch (IOException e) {
System.err.println("File mot Found!");
e.printStackTrace();
}
//try catch method to catch any errors
System.out.println("File Complete!");
//close file
Fwrite.close();

最佳答案

要显示未找到文件异常,将捕获块更改为此

catch(FileNotFoundException e){
e.printStackTrace();
}

关于java - 如果找不到文件,如何显示错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59123016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com