2014-04-05 52 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 /下。

希望这会有所帮助!

相关问题