gpt4 book ai didi

java - 保存自定义类时出现 NotSerializableException

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:52:18 25 4
gpt4 key购买 nike

我的两个类也设计为创建一个 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 方法打印到控制台,我假设我应该为每个循环使用一个是正确的吗?像这样?

                 //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界面。 (请注意,这是一个标记接口(interface),因此您不必实现任何方法。)

class Student implements Serializable {
}

要遍历 Student 对象的数组 S,试试这个:

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

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

Student[]  students = new Student[n];

那么你的 for 循环可以更具可读性。

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

请注意,您不必在类型为 Student 的对象上显式调用 toString 方法。当您将对象用作需要 String 的方法的参数时,Java 会隐式地为您做这件事。

关于java - 保存自定义类时出现 NotSerializableException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12959904/

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