2014-04-05 92 views
-1

我正在嘗試製作一個程序,該程序需要學生ID號,姓名和班級成績。我希望能夠讀取數據後。但是當我嘗試讀取數據(或打開文件)時出現錯誤。當我運行程序時,出現「Error opening file」錯誤。我有5個班,並將附加他們。有人可以試圖幫助我,並找出錯誤。我會附上類:我想打開我的文本文件,但我不知道問題是什麼

public class StudentRecords 
    { 
     private int IDnumber; 
     private String firstName; 
     private String lastName; 
     private double grade; 

     public StudentRecords() 
     { 
      this(0, "", "", 0.0); 

     } 

     public StudentRecords(int id, String first, String last, double gr) 
     { 
      setIDnumber(id); 
      setFirstName(first); 
      setLastName(last); 
      setGrade(gr); 
     } 
     public void setIDnumber(int id) 
     { 
      IDnumber = id; 
     } 

     public int getIDnumber() 
     { 
      return IDnumber; 
     } 

     public void setFirstName(String first) 
     { 
      firstName = first; 
     } 

     public String getFirstName() 
     } 
      return firstName; 
     } 

     public void setLastName(String last) 
     { 
      lastName = last; 
     } 

     public String getLastName() 
     { 
      return lastName; 
     } 

     public void setGrade(double gr) 
     { 
      grade = gr; 
     } 

     public double getGrade() 
     { 
      return grade; 

     } 
    } 

這裏是第二類

import java.io.FileNotFoundException; 
    import java.lang.SecurityException; 
    import java.util.Formatter; 
    import java.util.FormatterClosedException; 
    import java.util.NoSuchElementException; 
    import java.util.Scanner; 

    public class StudentTextFile 
    { 
     private Formatter output; 

     public void openFile() 
     { 
      try 
      { 
       output = new Formatter("students.txt"); 
      } 
      catch (SecurityException securityException) 
      { 
       System.err.println(
        "You do not have write access to this file."); 
       System.exit(1); 

      } 
      catch (FileNotFoundException fileNotFoundException) 
      { 
       System.err.println("Error opening or creating file."); 
       System.exit(1); 
      } 
     } 

     public void addStudentRecords() 
     { 
      StudentRecords record = new StudentRecords(); 
      Scanner input = new Scanner(System.in); 

      System.out.printf("%s\n%s\n%s\n%s\n\n", 
      "To terminate input, type the end-of-file indicator", 
      "when you are prompted to enter input.", 
      "On UNIX/Linux/Mac OS X type <ctrl> d then press Enter", 
      "On Windows type <crtl> z then press enter"); 

      System.out.printf("%s\n%s", 
     "Enter student ID number (> 0), first name, last name and grade. ", 
      "? "); 

      while (input.hasNext()) 
      { 
       try 
       { 
        record.setIDnumber(input.nextInt()); 
        record.setFirstName(input.next()); 
        record.setLastName(input.next()); 
        record.setGrade(input.nextDouble()); 

       if (record.getIDnumber() > 0) 
       { 
        output.format("%d %s %s %.2f\n", record.getIDnumber(), 
        record.getFirstName(), record.getLastName(), 
       record.getGrade()); 
       } 
       else 
       { 
       System.out.println(
         "Student ID Number must be greater than 0."); 
      } 
     } 
     catch (FormatterClosedException formatterClosedException) 
     { 
      System.err.println("Error writing to file."); 
      return; 
     } 
     catch (NoSuchElementException elementException) 
     { 
      System.err.println("Invalid input. Please try again."); 
      input.nextLine(); 

     } 
     System.out.printf("%s %s \n%s", "Enter student ID number (>0),", 
       "first name. last name and grade.", "? "); 
    } 
} 
public void closeFile() 
{ 
    if (output != null) 
     output.close(); 
} 

}

這裏是第三類

public class StudentTextFileTest { 

     public static void main(String[] args) 
     { 

      StudentTextFile application = new StudentTextFile(); 

      application.openFile(); 
      application.addStudentRecords(); 
      application.closeFile(); 

     } 

    } 

這裏是第四類

import java.io.File; 
    import java.io.FileNotFoundException; 
    import java.lang.IllegalStateException; 
    import java.util.NoSuchElementException; 
    import java.util.Scanner; 

    public class ReadStudentTextFile 
    { 
     private Scanner input; 

     public void openfile() 
     { 
      try 
      { 
       input = new Scanner(new File("studentrecords.txt")); 
      } 
      catch (FileNotFoundException fileNotFoundException) 
      { 
       System.err.println("Error opening file."); 
       System.exit(1); 

      } 
     } 

     public void readStudentRecords() 
     { 
      StudentRecords record = new StudentRecords(); 

      System.out.printf("%-10s%-12s%-12s%10s\n", "Student ID Number", 
      "First Name", "Last Name", "Balance"); 

     try 
     { 
      while (input.hasNext()) 
      { 
       record.setIDnumber(input.nextInt()); 
       record.setFirstName(input.next()); 
       record.setLastName(input.next()); 
       record.setGrade(input.nextDouble()); 

       System.out.printf("%-10d%-12s%-12s%10.2f\n", 
       record.getIDnumber(), record.getFirstName(), 
       record.getLastName(), record.getGrade()); 
      } 
     } 
     catch (NoSuchElementException elementException) 
     { 
      System.err.println("File improperly formed. "); 
      input.close(); 
      System.exit(1); 

     } 
     catch (IllegalStateException stateException) 
     { 
      System.err.println(" Error reading from file."); 
      System.exit(1); 
     } 
     } 
     public void closeFile() 
     { 
      if (input!= null) 
       input.close(); 
     } 
    } 

這裏是第五類

 public class ReadStudentTextFileTest 
    { 

     public static void main(String[] args) 
     { 

      ReadStudentTextFile application = new ReadStudentTextFile(); 

      application.openfile(); 
      application.readStudentRecords(); 
      application.closeFile(); 



     } 

    } 
+0

該文件是否存在,您確定您的文件路徑正確嗎? – PakkuDon

+0

當發生'FileNotFoundException'時,您自己的代碼會打印該字符串。 – keyser

+3

您已經發布了很多與您的問題無關的代碼。嘗試縮小問題的位置。本文應該幫助你:http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – SimonC

回答

0

而是創建一個文件的每一個從它讀的時候,你應該嘗試使用包裹在一個BufferedReader一個的FileReader。

更改此:

input = new Scanner(new File("studentrecords.txt")); 

要:

input = new Scanner(new BufferedReader(new FileReader("studentrecords.txt"))); 

更多信息,請參見tutorial for the Scanner class

還要確保您正在嘗試讀取的文件存在。也就是說,它應該與代碼位於同一個文件夾中。另一個簡單的選擇是嘗試提供文件的完整路徑。

1

我第二次試圖把整個路徑,而不是。

對於相對路徑,如果要編譯並在命令行/終端中運行,則需要將該studentrecords.txt放在代碼文件的同一目錄中。

如果您使用IDE,則需要將您的studentrecords.txt放在src /文件夾下。這假設你不在Maven項目設置中;否則應該放在/ src/main/java/resources /下。

希望這會有所幫助!

相關問題