Я новичок в Java, и я учусь десериализации, и при этом я получаю classcastexception
Я сделал сериализацию в одном классе и десериализации в другом
сериализация
zipfile f = new zipfile(30,"kavin");
ArrayList<zipfile> a = new ArrayList<zipfile>(101);
a.add(f);
String file = "def.txt";
try {
FileOutputStream fi = new FileOutputStream(file);
ObjectOutputStream s = new ObjectOutputStream(fi);
s.writeObject(f);
System.out.println(f.age);
s.close();
fi.close();
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
десериализация
String file = "def.txt";
try {
FileInputStream fi = new FileInputStream(file);
ObjectInputStream s = new ObjectInputStream(fi);
f=(deserialization)s.readObject();
System.out.println(f.age);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
output
Исключение в потоке "main" java.lang.ClassCastException: файл demo2.zip не может быть приведен к demo2.deserialization вdemo2.deserialization.main (deserialization.java:69)