gpt4 book ai didi

java - 序列化 - readObject writeObject 覆盖

转载 作者:IT老高 更新时间:2023-10-28 20:53:19 25 4
gpt4 key购买 nike

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

问题是我不完全理解被要求做什么。我已阅读 Uses of readObject/writeObject in Serialization但我无法理解它。有人能指出我正确的方向吗?

我的代码:

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 在创建 Student 实例后立即被调用(绕过正常的构造函数)。

关于java - 序列化 - readObject writeObject 覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12963445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com