2016-11-08 58 views
0

我从学生数据库问题中读取二进制值时遇到了很多麻烦。经过一些修改后,我得到了输出正常工作,并在格式中,我想即(int id,字符串名称,int年龄),但是,我有很多麻烦从我的文件加载编码的二进制数据值。根据编译器,它一直读取直到UTF值,然后我收到一个EOFException。EOFException从二进制文件,学生数据库中读取UTF文件

这里是我试图完成的问题:

首先,启动程序后,用户选择的选项要么 1.学生添加到数据库 2.从学生数据库 3.在数据库中打印一个特定的学生 4.打印数据库中的所有学生

如果他们选择,用户将为学生添加一个Student obj。 学生obj包含一个ID(int),一个名称(字符串)和一个年龄(int)。

删除学生根据用户输入的ID号码将其从矢量中删除。

和选项3/4打印学生的obj(S)到屏幕本身(而不是二进制文件)

用户退出后,通过输入5的方案;向量中的学生obj被写入二进制文件。

当程序重新运行时,它会将学生obj加载回向量中。 (记住该文件被写入并从一个二进制文件加载)

我遇到的问题是当文件“Student.data”(我正在写入的文件)正在加载它不断扔我当我尝试读取学生姓名时,第198行处发生EOFException。

我相信问题可能出现在编写代码时,但我修改了我抓取名称值并多次写入的方式,但仍然无法正确加载它们。

有关如何解决此问题的任何建议?

代码:

//StudentDB.java 
import java.util.Vector; 
import java.io.*; 
import java.util.Scanner; 
class Student 
{ 
    private int id; 
    private String name; 
    private int age; 
    //Desc: Initializes the student to id=0, name="none", and age=0 
    public Student() 
    { 
     this.id=0; 
     this.name="none"; 
     this.age=0; 
    } 
    //Desc: Initializes the student to id=i, name=n, age=a 
    public Student(int I, String n, int a) 
    { 
     this.id=I; 
     this.name=n; 
     this.age=a; 
    } 
    //Desc: Sets the current id to s 
    public void setID(int s) 
    { 
     this.id=s; 
    } 
    //Return: The current ID value 
    public int getID() 
    { 
     return this.id; 
    } 
    //Desc: sets the current name of the student to s 
    public void setName(String s) 
    { 
     this.name=s; 
    } 
    //Return: The current name of the student 
    public String getName() 
    { 
     return this.name; 
    } 
    //Desc: sets the current age of the obj to a 
    public void setAge(int a) 
    { 
     this.age=a; 
    } 
    //Return: The students current age 
    public int getAge() 
    { 
     return this.age; 
    } 
    //Return: True if id of this student equals id of obj, false otherwise 
    public boolean equals(Object obj) 
    { 
     Student stu= (Student)obj; 
     if(this.id==stu.id) return true; 
     else return false; 
    } 
    //Desc: Compares two students based on their ID to determine if they're 
    // the same 
    //Return: 1 if the current Student's ID is greater than stu 
    //  0 if the students ID's are the same 
    //  -1 if Student stu's ID is larger 
    public int compareTo(Student stu) 
    { 
     if(this.id> stu.id) return 1; 

     else if(this.id==stu.id) return 0; 

     else return -1; 

    } 
    //Return: id+"Name"+age 
    public String toString() 
    { 
      return (this.getID()+ " "+this.getName()+" "+this.getAge()); 
    } 
} 
public class StudentDB 
{ 
    private static Scanner keyboard=new Scanner(System.in); 
    //Desc: Maintains a database of Student Records. The database is stored in 
    // a binary file called "Student.data" 
    //Input: User enters commands from keyboard to manipulate database 
    //Output: Database updated as directed by user. 
    public static void main(String[]args) throws IOException 
    { 
     Vector<Student> v= new Vector<Student>(); 
     File s= new File("Student.data"); 
     if(s.exists()) loadStudent(v); 
     int choice=5; 
     do 
     { 
      System.out.println("\t1. Add a Student Record"); 
      System.out.println("\t2. Remove a Student Record"); 
      System.out.println("\t3. Print a Student Record"); 
      System.out.println("\t4. Print all Student Records"); 
      System.out.println("\t5. Quit"); 
      choice= keyboard.nextInt(); 
      keyboard.nextLine(); 
      switch(choice) 
      { 
       case 1: addStudent(v); break; 
       case 2: removeStudent(v); break; 
       case 3: printStudent(v); break; 
       case 4: printAllStudent(v); break; 
       default: break; 
      } 
     } while(choice!=5); 
     storeStudent(v); 
    } 
    //Input: user enters an integer(id), a string(name), an integer(age) from 
    //the keyboard all on seperate lines 
    //Post: The input record added to v if id does not exist 
    //Output: various prompts as well as "Student Added" or "Add failed: 
    // Student already exists" printed on the screen accordingly 
    public static void addStudent(Vector<Student> v) 
    { 
     Student stu= new Student(); 

     System.out.print("Please enter a Student ID:"); 
     stu.setID(keyboard.nextInt()); 
     keyboard.nextLine(); 

     System.out.print("Please enter a Student Name:"); 
     stu.setName(keyboard.nextLine()); 

     System.out.print("Please enter a Student Age:"); 
     stu.setAge(keyboard.nextInt()); 
     keyboard.nextLine(); 

     int index= v.indexOf(stu); 
     if(index==-1) 
     { 
      v.add(stu); 
      System.out.println("Student Added"); 
     } 
     else System.out.println("Add failed: Student already exists"); 

    } 
    //Input: user enters an integer(id) from the Keyboard 
    //Post: The records in v whose id field matches the input removed from v 
    //Output: various prompts as well as "Student removed" or "Remove failed: 
    // Student does not exsist" printed on the screen accordingly 
    public static void removeStudent(Vector<Student>v) 
    { 
     System.out.print("Student ID:"); 
     int id= keyboard.nextInt(); 
     Student stu= new Student(id,"",99); 
     if(v.remove(stu)) System.out.println("Student Removed"); 
     else System.out.println("Remove Failed"); 
    } 
    //Input: user enters an integer(id) from the Keyboard 
    //Output: various prompts as well as the record in v whose id field 
    // matches the input printed on the screen or "Print failed: Student 
    // does not exsist" printed on the screen accordingly 
    public static void printStudent(Vector<Student>v) 
    { 
     System.out.print("Student ID:"); 
     int id= keyboard.nextInt(); 
     Student stu= new Student(id,"",99); 
     int index=v.indexOf(stu); 
     if(index!=-1) 
     { 
      System.out.println(v.get(index).toString()); 
     } 
     else System.out.println("Print failed: Student does not exsist!"); 
    } 
    //Output: All records in v printed on the screen 
    public static void printAllStudent(Vector<Student>v) 
    { 
     Student stu=new Student(); 
     for(int i=0; i<v.size();++i) 
     { 
      stu=v.get(i); 
      System.out.println(stu); 
     } 

    } 
    //Input: Binary file Student.data must exist and contains student records 
    //Post: All records in Student.data loaded into vector v. 
    public static void loadStudent(Vector<Student>v)throws IOException 
    { 
     Student stu; 
     int c; 
     DataInputStream f= new DataInputStream(
       new FileInputStream("Student.data")); 
     try { 
      while(true) 
      { 
       stu=new Student(); 
       int id=f.readInt(); 
       stu.setID(id); 
       String name= f.readUTF(); 
       stu.setName(name); 
       int age= f.readInt(); 
       stu.setAge(age); 
       v.add(stu); 
      } 
     } 
     catch(EOFException e) 
     { 
      System.out.println("Error!: End of File Exception"); 
      f.close(); 
     } 
     catch(IOException e) 
     { 
      System.out.println("Error Reading File"); 
      f.close(); 
      System.exit(1); 
     } 
     f.close(); 
    } 
    //Output: All records in v written to binary file Student.data 
    public static void storeStudent(Vector<Student>v) throws IOException 
    { 
     Student stu= new Student(); 
     DataOutputStream s= new DataOutputStream(
       new FileOutputStream("Student.data")); 
     for(int i=0; i<v.size(); ++i) 
     { 
      stu=v.get(i); 
      s.writeInt(stu.getID()); 
      s.writeUTF(stu.getName()); 
      s.writeInt(stu.getAge()); 
     } 
     s.close(); 
    } 
} 
+0

编辑出你的名字和电子邮件大声笑。还提供样本用法。遇到错误的'main'方法。 – JordanGS

+0

由于文件中存在随机垃圾字符,请查看十六进制编辑器并亲自查看。您不必删除学生导致错误。你所要做的就是添加一个学生。这意味着您的写入操作很糟糕。 – JordanGS

回答

0

两个原因是它打破。

  1. 你有无限的读
  2. 你每一个学生追加到同一行

做如下修改

loadStudent变化white(true)while(f.available() > 0)并在末尾添加f.readChar();

public static void loadStudent(Vector<Student>v)throws IOException 
{ 
    Student stu; 
    int c; 
    DataInputStream f= new DataInputStream(
      new FileInputStream("Student.data")); 
    try { 
     // CHANGE HERE IS IMPORTANT 
     while(f.available() > 0) 
     { 
      stu=new Student(); 
      int id=f.readInt(); 

      stu.setID(id); 
      String name= f.readUTF(); 
      stu.setName(name); 
      int age= f.readInt(); 
      stu.setAge(age); 
      v.add(stu); 
      // This read char is important since it's one user per line. 
      f.readChar(); 
     } 
    } 
    catch(EOFException e) 
    { 
     System.out.println("Error!: End of File Exception"); 
     f.close(); 
    } 
    catch(IOException e) 
    { 
     System.out.println("Error Reading File"); 
     f.close(); 
     System.exit(1); 
    } 
    f.close(); 
} 

storeStudent当保存的学生,在末尾添加一个新行字符。s.writeChar('\n');

public static void storeStudent(Vector<Student>v) throws IOException 
{ 
    Student stu= new Student(); 
    DataOutputStream s= new DataOutputStream(
      new FileOutputStream("Student.data")); 
    for(int i=0; i<v.size(); ++i) 
    { 
     stu=v.get(i); 
     s.writeInt(stu.getID()); 
     s.writeUTF(stu.getName()); 
     s.writeInt(stu.getAge()); 
     // One user per line 
     s.writeChar('\n'); 
     } 
    s.close(); 
}