Person class
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);
}
}
Serialization
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!!");
}
}
Deserialization
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();
}
}
I am trying to learn some FILE handling in java and been stuck on this for 2 hours, I did multiple examples and the same exception comes out. When I try to deserialize the whole file as a list, I get a class cast exception
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)
If i use fileList.add(objectReader.readObject()); I only get the first one from the file and it is working but I only get the first one.
Any solution would be helpful.
EDIT: I used a whole list at once with conditioned objects and it all worked thanks