Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
466 views
in Technique[技术] by (71.8m points)

java - 如果找不到文件,如何显示错误消息?(How do I display an error message if a file is not found?)

I wrote a program to read from a specific file and was wondering how would I display a custom message if that file were to not be found.

(我编写了一个程序来读取特定文件,并且想知道如果找不到该文件该如何显示自定义消息。)

What I have currently does not work, could someone explain why?

(我目前没有的东西,有人可以解释为什么吗?)

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

Highest Test Score: " + highestScore + "
");

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

       //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() + "
");
           }
       }

   //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();
  ask by ZootZoot translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To show file not found exception change catch block to this

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...