我想从 file 读取不同类型的对象到 an ArrayList
,它们都是类的实例,它们扩展了类 Advertisement。我正在尝试使用此代码:
ArrayList <Advertisement> ads = new ArrayList<Advertisement>();
ObjectInput input2 = new ObjectInputStream(
new BufferedInputStream(new FileInputStream("ads.ser")));
//break from this method at this point
ads = (ArrayList<Advertisement>) input2.readObject();
问题出在第三行/最后一行。它不会读取ads
类型变量中 的对象ArrayList<Advertisement>
,此外,它会从这个方法中中断,没有任何消息。
编辑:删除了 try-catch 块,我应该早点完成,但我仍然不知道解决方案。堆栈跟踪:
java.io.InvalidClassException: Kiado; local class incompatible: stream classdesc serialVersionUID = -1393576200767336208, local class serialVersionUID = -841663850423605586
at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at java.util.ArrayList.readObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at Main.beolvasas(Main.java:30)
at Main.startup(Main.java:57)
at Main.main(Main.java:633)