问题描述
我正在尝试编写一种方法来反序列化我称为Student
的对象。方法如下:
public void readStudentInfo() {
// Desrializes the student objects into an ArrayList for you to compare against
try{
FileInputStream fis = new FileInputStream("student.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
deserializedStudents = (ArrayList) ois.readobject();
ois.close();
fis.close();
}
catch(IOException ioe){
ioe.printstacktrace();
return;
} catch(ClassNotFoundException c){
System.out.println("Class not found");
c.printstacktrace();
return;
}
for(Student student : deserializedStudents){
System.out.println(student.toString());
}
}
与之一起使用的Student类如下:
import java.io.Serializable;
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
// Class Attributes
private String studentID;
private String rankings;
private char personalityType;
private String conflict;
private String preferences;
// Class Constructor
public Student(String ID){
this.studentID = ID;
}
public Student(String ID,String grades) {
this.studentID = ID;
grades = grades.trim();
this.rankings = grades;
}
public Student(String ID,String ranking,char personality){
this.studentID = ID;
this.rankings = ranking;
this.personalityType = personality;
}
// Accessor Methods
public String getStudentID() {
return this.studentID;
}
public String getRankings(){
return this.rankings;
}
public String getPreferences(){
return this.preferences;
}
public char getPersonalityType(){
return this.personalityType;
}
public String getConflict(){
return this.conflict;
}
//Modifier Methods
public boolean setPreferences(String pref){
this.preferences = pref;
return true;
}
public boolean setGrades(String grades){
this.rankings = grades;
return true;
}
public boolean setPersonalityType(char pers){
this.personalityType = Character.toupperCase(pers);
return true;
}
public boolean setConflict(String ids){
this.conflict = ids;
return true;
}
@Override
public String toString(){
return studentID + ";" + rankings + ";" + personalityType + ";" + conflict + ";" + preferences;
}
}
ArrayList deserializedStudents
在类的顶部启动,如下所示:ArrayList<Student> deserializedStudents = new ArrayList<>();
出于某种原因,readStudentInfo()
的以下行发出此警告:类型安全:ArrayList类型的表达式需要未经检查的转换才能符合ArrayListJava(16777748)
如果我忽略它并无论如何运行程序,则readStudentInfo()
方法将出现以下错误:Exception in thread "main" java.lang.classCastException: class Student cannot be cast to class java.util.ArrayList (Student is in unnamed module of loader 'app'; java.util.ArrayList is in module java.base of loader 'bootstrap')
有人可以帮助我了解为什么会这样吗?我试过几次在下面的行中更改演员表:deserializedStudents = (ArrayList) ois.readobject();
。我还尝试过在程序开始时在一些启动ArrayList的地方进行一些更改,但是还没有运气。
澄清:这里的基本思想是我试图将一堆Student类型的对象反序列化到ArrayList中,以便我可以再次使用它们。
请帮助!
解决方法
似乎您没有序列化学生列表(这是一个自己的对象),而是一个一个地序列化了学生。因此,您应该序列化一个列表,或者一个接一个地反序列化Student(通过连续调用readObject())。
尝试此操作(使用try-with-resources):
public void readStudentInfo() {
try (FileInputStream fis = new FileInputStream("student.ser");
ObjectInputStream ois = new ObjectInputStream(fis)) {
List<Student> deserializedStudents = new ArrayList<>();
while (fis.available() > 0) {
deserializedStudents.add((Student) ois.readObject());
}
deserializedStudents.foreach(student -> System.out.println(student));
} catch(IOException | ClassNotFoundException exc){
exc.printStackTrace();
}
}