序列化 - 读取对象写入对象重写

2022-09-01 05:04:22

写完下面的代码后,我现在必须在学生数据中使用自定义的readObject()和writeObject()覆盖方法来读取和写入对象的变量。不使用 defaultWriteObject 或 defaultReadObject 方法来执行此操作。

问题是我不完全理解我被要求做什么。我已经阅读了序列化中读取对象/写入对象的用法,但我无法理解它。有人能给我指出正确的方向吗?

我的代码:

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

class Student implements java.io.Serializable {

    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";
    }

}
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[]  students = new Student[n];

        //Student[]  S=new Student[n];                      // array of objects declared and defined
        for (int i = 0; i < students.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());

            students[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(students);

                students = null;

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

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

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

答案 1

你必须这样做:

import java.io.IOException;

class Student implements java.io.Serializable {

    String name;
    String DOB;
    int id;

    Student(String naam, int idno, String dob) {
        name = naam;
        id = idno;
        DOB = dob;
    }

    private void writeObject(java.io.ObjectOutputStream stream)
            throws IOException {
        stream.writeObject(name);
        stream.writeInt(id);
        stream.writeObject(DOB);
    }

    private void readObject(java.io.ObjectInputStream stream)
            throws IOException, ClassNotFoundException {
        name = (String) stream.readObject();
        id = stream.readInt();
        DOB = (String) stream.readObject();
    }

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

}

读取对象是在创建 Student 的实例(绕过常规构造函数)后立即调用的。


答案 2

我知道这个问题很老了,为子孙后代考虑这个问题

通常,您可以通过允许所有“正常”字段自动反序列化来让 JVM 完成艰苦的工作:

private void readObject(ObjectInputStream serialized) throws ClassNotFoundException, IOException 
{
    serialized.defaultReadObject();
    // After this, you can handle transient fields or 
    // special initialization that happens in the constructor
}

的文档对此非常清楚:defaultReadObject()

从此流中读取当前类的非静态和非瞬态字段。这只能从被反序列化的类的 readObject 方法中调用。如果以其他方式调用 NotActiveException,它将引发它。