保存自定义类的NotSerializableException

时间:2012-10-18 17:01:10

标签: java serialization notserializableexception

我的两个类也被设计为创建一个StudentData对象数组(名称,出生日期和ID),其中包括toString覆盖以打印出所有变量。然后它将数组序列化并将其保存到名为studentdata.txt的文件中。然后它应该从文件中读取数组并从该数据重建一个新的数组对象,并将数组中的每个项打印到控制台。

尝试编译时出现此错误...

Exception in thread "main" java.io.NotSerializableException: Student
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeArray(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at StudentData.main(StudentData.java:38)

另外我不确定如何正确循环我的数组并调用我的toString方法打印到控制台,我是否正确假设我应该为每个循环使用a?喜欢这个?

                 //for (Student s : ???) {
                  //System.out.println(How do I call toString from here?);

我的班级

import java.io.*;         //importing input-output files
    class Student
    {


   String name;                     //declaration of variables
 String DOB;
   int id;



   Student(String naam,int idno, String dob)          //Initialising variables to user data
   { 
          name=naam;
          id=idno;
          DOB=dob;

    }

   public String toString() {
       return name+"\t"+id+"\t"+DOB+"\t";
}



}

2号

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

class StudentData                     //main class
{
  public static void main(String args[]) throws IOException                  //exception handling
  {
         System.out.println("Enter the numbers of students:");
         BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
         int n=Integer.parseInt(in.readLine());


         Student[]  S=new Student[n];                      // array of objects declared and defined
         for (int i = 0; i < S.length; i++)       {

               System.out.println("Enter the Details of Student no: "+(i+1));             //reading data form the user
               System.out.println("Name: ");
               String naam=in.readLine();
               System.out.println("ID no: ");
               int idno=Integer.parseInt(in.readLine());
               System.out.println("DOB: ");               
               String dob=(in.readLine());


              S[i]=new Student(naam,idno,dob);                          

              File studentFile = new File("StudentData.txt");
              try {
              FileOutputStream fileOutput = new FileOutputStream(studentFile);
              ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput);
              objectOutput.writeObject(S);

              S = null;

              FileInputStream fileInput = new FileInputStream(studentFile);
              ObjectInputStream objectInputStream = new ObjectInputStream(fileInput);

                S = (Student[]) objectInputStream.readObject();
            } catch (ClassNotFoundException e) {

                e.printStackTrace();
            }

              //for (Student s : ???) {
                  //System.out.println();
              }

         }








     }

3 个答案:

答案 0 :(得分:2)

您的班级Student应实施Serializable界面以制作其实例serializable

将您的Student课程声明编辑为: -

class Student implements java.io.Serializable {
}

请参阅此链接: - http://docs.oracle.com/javase/7/docs/platform/serialization/spec/serial-arch.html详细讲述Serialization

答案 1 :(得分:2)

要摆脱NotSerializableException: Student异常,只需让Student类实现Serializable接口即可。 (请注意,这是一个标记接口,因此您不必实现任何方法。)

class Student implements Serializable {
}

要遍历SStudent个对象的数组,请尝试以下操作:

for (Student st : S) {
    System.out.println(st);
}

我会为数组选择一个更具描述性的名称(students会更好)。如果将数组声明为

Student[]  students = new Student[n];

然后你的for循环可以更具可读性。

for (Student student : students) {
    System.out.println(student);
}

请注意,您不必在toString类型的对象上显式调用Student方法。当您将对象用作期望String的方法的参数时,Java将隐式执行此操作。

答案 2 :(得分:1)

实现您的Student类java.io.Serializable接口。