为什么在尝试反序列化列表时会出现ClassCastException



人员类

package model;
import java.io.Serializable;
import java.util.Objects;
public class Person implements Serializable {
private String name;
private String hobby;
private Integer weight;
private Integer age;
public Person(String name, String hobby, Integer weight, Integer age) {
this.name = name;
this.hobby = hobby;
this.weight = weight;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return
"Name = '" + name + "'n" +
"Hobby = '" + hobby + "'n" +
"Weight = " + weight + "'n" +
"Age = " + age + "'n";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(name, person.name) && Objects.equals(hobby, person.hobby) && Objects.equals(weight, person.weight) && Objects.equals(age, person.age);
}
@Override
public int hashCode() {
return Objects.hash(name, hobby, weight, age);
}
}

序列化

public static void serialization(List<Person> fileList){
if(fileList.size() > 0) {
try (ObjectOutputStream writer = new ObjectOutputStream(new FileOutputStream("dat/serialPersons.ser"))) {
for (int i = 0; i < fileList.size(); i++) {
if (fileList.get(i).getWeight() < 80) {
writer.writeObject(fileList.get(i));
}
}
System.out.println("Serilization complete!");
} catch (FileNotFoundException ex) {
System.err.println(ex);
} catch (IOException ex) {
System.err.println(ex);
}
}
else{
System.out.println("List is empty!!");
}
}

反序列化

public static void deserialization(){
try(ObjectInputStream objectReader
= new ObjectInputStream(new FileInputStream("dat/serialPersons.ser"))) {
List<Person> deserializedList = (List<Person>)objectReader.readObject();
deserializedList.forEach(System.out::println);
} catch(IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

我正在尝试学习java中的一些FILE处理,已经坚持了2个小时,我做了多个例子,结果出现了相同的异常。当我试图将整个文件反序列化为列表时,我会得到一个类强制转换异常

Exception in thread "main" java.lang.ClassCastException: class model.Person cannot be cast to class java.util.List (model.Person is in unnamed module of loader 'app'; java.util.List is in module java.base of loader 'bootstrap')
at main.Main.deserialization(Main.java:115)
at main.Main.main(Main.java:32)

如果我使用fileList.add(objectReader.readObject(((;我只从文件中得到第一个,它正在工作,但我只得到第一个。

任何解决方案都会有所帮助。

编辑:我同时使用了一整张带有条件对象的列表感谢

我想你在写"人";对象和"的阅读列表;"人";对象"writer.writeObject(fileList.get(i(("该代码将编写单个Person,因为fileList.get(index(返回单个形式的Person,但是在阅读部分,您希望该对象是"的列表;人";。

为了解决这个问题,微小的修改可以节省流量,至少值得一试。但我不确定你的整个流程,这只是一个建议。希望能帮助您:(

writer.writeObject(Arrays.asList(fileList.get(i)));

异常表示您正试图将Person的实例强制转换为List(Person(。这是因为readObject返回一个Object(在本例中为一个Person(。要创建Person列表,请使用适当的分隔符(逗号、制表符等(拆分文件,然后一次读取一个Object。或者,您必须定义另一个类,它可以像Person数组一样工作(例如,称之为PersonList(,并使用readObject来创建PersonList实例。或者以ArrayList(或您喜欢的其他一些List实现(的形式编写并阅读Person的ArrayList就可以完成这项工作。

AS it:如何将多个对象写入可序列化文件,并在再次使用程序时读取它们?

您试图将一个人列入列表<个人>。

在序列化期间,直接写入数组列表。

序列化

public static void serialization(List<Person> fileList){
if(fileList.size() > 0) {
// First create the list of persons to save
List<Person> personsToSave = new ArrayList<Person>();
try (ObjectOutputStream writer = new ObjectOutputStream(new FileOutputStream("dat/serialPersons.ser"))) {
for (int i = 0; i < fileList.size(); i++) {
if (fileList.get(i).getWeight() < 80) {
personsToSave.add(fileList.get(i));
}
}
// Then save
writer.writeObject(personsToSave);
System.out.println("Serilization complete!");
} catch (FileNotFoundException ex) {
System.err.println(ex);
} catch (IOException ex) {
System.err.println(ex);
}
}
else{
System.out.println("List is empty!!");
}
}

最新更新