保存自定义类的NotSerializable Exception



我的两个类也是设计的,创建了一系列StudentData对象(名称,出生日期和ID),其中包括tostring覆盖以打印所有变量。然后,它序列化数组并将其保存到名为Sut剂Data.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();
              }
         }




     }

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

class Student implements Serializable {
}

要通过Student对象的数组S循环,请尝试以下操作:

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

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

Student[]  students = new Student[n];

那么您的循环可以更可读。

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

请注意,您不必在类型Student的对象上明确调用toString方法。当您将对象用作期望String的方法的参数时,Java将为您隐含地做到这一点。

您的类Student应该实现Serializable接口以使其实例serializable

编辑您的Student类声明为: -

class Student implements java.io.Serializable {
}

请参阅此链接:-http://docs.oracle.com/javase/7/docs/platform/serialization/serialization/serial-ark.html详细讨论Serialization

实现您的学生类Java.io.serializable接口。

最新更新