DAT不保存整个对象JAVA

时间:2016-05-09 17:12:41

标签: java io binary

嘿我试图将对象保存到.dat。我试图保存的对象是一个学生,参数为Student [usernmae,password,name,idnum,major,gpa]。当我创建一个新对象时,所有参数都被填入,当我保存它全部填写。但是当我加载对象时,它加载的是用户名和密码。就像它甚至不能保存其余部分一样。任何人有任何修复?

Loader class:

public static void loader()throws IOException, ClassNotFoundException{
 try {
 FileInputStream fis = new FileInputStream("students.dat");
 ObjectInputStream ois = new ObjectInputStream(fis);
 //a loop to continue reading the next line 
 //of a binary file for the next student
 while(true){
   try {
    stud = ois.readObject();

  }catch(EOFException e){
    break;
  }  
}
  student = (Student) stud;
  System.out.println(student);
  studentBag.add(student);
  ois.close();
  fis.close();
}catch(FileNotFoundException e) {
  System.out.println("File not found");
}
}

Saver Class:

public static void saver(Student student)throws IOException{
try {
 FileOutputStream fos = new FileOutputStream("students.dat");
 ObjectOutputStream oos = new ObjectOutputStream(fos);
 oos.writeObject(student);
 System.out.println(student.toString());
 oos.flush();
 oos.reset();
 oos.close();
 fos.close();
}catch (FileNotFoundException e) {
 System.out.println("File not found");
}
}

是的,我的学生对象是可序列化的。“private static final long serialVersionUID = 1L;

当我从文件中打印出学生时,这是输出:

学生[name = null,userName = 111,密码= 111,majorID = null,gpa = 0.0]

`

0 个答案:

没有答案
相关问题