在构造函数参数中使用ArrayList时出现InvalidClassException

时间:2015-11-27 20:38:58

标签: java serialization arraylist

当我在构造函数的参数中写入一个带有ArrayList的对象(即使我什么都不对它)到一个文件并将其读回来时,它会抛出一个InvalidClassException,并说对象的serialVersionUID为&# 39; t匹配,但是从构造函数中取出它将允许它再次正常运行。可能导致此错误的原因是什么?

这个构造函数可以正常工作

public Post(String poster, String postName, int imageWidth, int imageHeight){
    this.poster = poster;
    this.postID = generateID();
    this.postName = postName;
    this.imageWidth = imageWidth;
    this.imageHeight = imageHeight;
}

然而这个会引发错误

public Post(String poster, String postName, ArrayList keys, int imageWidth, int imageHeight){
    this.poster = poster;
    this.postID = generateID();
    this.postName = postName;
    this.imageWidth = imageWidth;
    this.imageHeight = imageHeight;
}

我将它保存到文件的方式只是使用带有FileOutputStream作为参数的ObjectOutputStream,并使用带有FileInputStream的ObjectInputStream读取它

这里是堆栈跟踪

11-27 15:34:50  java.io.InvalidClassException: net.gladiatorlife.aoit.test.Post; local class incompatible: stream classdesc serialVersionUID = -4349899324072111291, local class serialVersionUID = 5190858650678115513
11-27 15:34:50      at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:617)
11-27 15:34:50      at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1622)
11-27 15:34:50      at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1517)
11-27 15:34:50      at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
11-27 15:34:50      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
11-27 15:34:50      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
11-27 15:34:50      at net.gladiatorlife.aoit.test.util.FileUtil.readFromFile(FileUtil.java:48)
11-27 15:34:50      at net.gladiatorlife.aoit.test.order.request.LatestOrder.handle(LatestOrder.java:45)
11-27 15:34:50      at net.gladiatorlife.aoit.test.Connection$1.run(Connection.java:121)
11-27 15:34:50      at java.lang.Thread.run(Thread.java:745)

修改 通过手动将可序列化ID设置为-4349899324072111291L

解决了问题

0 个答案:

没有答案