序列化 - readObject writeObject覆盖 [英] Serialization - readObject writeObject overrides

查看:167
本文介绍了序列化 - readObject writeObject覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Having written the code below I now have to,use custom readObject() and writeObject() override methods in StudentData to read and write the object's variables. Without using the defaultWriteObject or defaultReadObject methods to do this.

问题是我不完全理解要求做什么。我已阅读序列化中使用readObject / writeObject 但我无法获得我的头围着它。有人能指出我正确的方向吗?

The trouble is that I don't understand fully what am being asked to do. I have read Uses of readObject/writeObject in Serialization but I can't get my head around it.Can someone point me in the right direction?

我的代码:

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


推荐答案

你必须这样做:

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

}

在创建后立即调用readObject学生的一个实例(绕过普通的构造函数)。

The readObject is invoked just after creating an instance of Student (bypassing the normal constructor).

这篇关于序列化 - readObject writeObject覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆