Java 保存自定义类时发生NotSerializableException
我的两个类也被设计用来创建StudentData对象(姓名、出生日期和ID)数组,其中包括一个toString覆盖以打印出所有变量。然后,它序列化数组并将其保存到名为studentdata.txt的文件中。然后,它应该从文件中读取数组,并根据该数据重建一个新的数组对象,然后将数组中的每个项打印到控制台 我在尝试编译时遇到此错误Java 保存自定义类时发生NotSerializableException,java,serialization,notserializableexception,Java,Serialization,Notserializableexception,我的两个类也被设计用来创建StudentData对象(姓名、出生日期和ID)数组,其中包括一个toString覆盖以打印出所有变量。然后,它序列化数组并将其保存到名为studentdata.txt的文件中。然后,它应该从文件中读取数组,并根据该数据重建一个新的数组对象,然后将数组中的每个项打印到控制台 我在尝试编译时遇到此错误 Exception in thread "main" java.io.NotSerializableException: Student at java.io.O
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";
}
}
二号
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();
}
}
}
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.io.ObjectInputStream;
导入java.io.ObjectOutputStream;
class StudentData//main class
{
公共静态void main(字符串args[])引发IOException//异常处理
{
System.out.println(“输入学生人数:”);
BufferedReader in=新的BufferedReader(新的InputStreamReader(System.in));
int n=Integer.parseInt(in.readLine());
Student[]S=new Student[n];//声明和定义的对象数组
对于(int i=0;i
您的类学生
应该实现接口,使其实例可序列化
编辑您的学生
班级声明,以便:-
class Student implements java.io.Serializable {
}
请参阅此链接:-它详细讨论了
序列化实现学生类java.io.Serializable接口。要摆脱notserializableeexception:Student
异常,只需让你的Student
类实现该接口即可。(请注意,这是一个标记接口,因此不必实现任何方法。)
要循环遍历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
方法。当您将对象用作需要字符串的方法的参数时,Java会隐式地为您执行此操作。请对代码块使用一致的逻辑缩进。谢谢帮助!我了解imp或者说,它是为了让代码能够被读取…这样对每个人来说都更容易!
for (Student student : students) {
System.out.println(student);
}