gpt4 book ai didi

Java序列化和反序列化

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:45:16 27 4
gpt4 key购买 nike

我有一个对象如下:

    public class Records implements java.io.Serializable{
private int cId;
private int pId;
private int vlaue;
private int tag;

public Records(int c, int p, int v, int t){
this.cId=c;
this.pId=p;
this.value=v;
this.tag=t;
}
}

我收集了很多数据,像上面的类一样构造了对象,并将它们序列化到磁盘。

我忘记在类文件中包含的转储内容是访问每个对象的值的方法。例如,访问特定对象的 cId 值。

我修改了类定义以添加此类方法,但随后我无法将对象反序列化回 Records 类并得到此运行时错误:

java.io.InvalidClassException: Records; local class incompatible: stream classdesc serialVersionUID = -1232612718276774474, local class serialVersionUID = -8244963718951538904
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:579)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1600)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1513)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1749)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1346)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:368)
at DeSerialise.main(DeSerialise.java:21)

我想我需要告诉 java 它们是相同 定义并修改 serialVersionUID 但不太确定如何修改?欢迎任何想法!

最佳答案

尝试将以下内容添加到您的类(class)中:

private static final long serialVersionUID = -1232612718276774474L;

这将使您的类的 serialVersionUID 与序列化实例时使用的编译器生成的值一致。

以下引自documentation值得一读(强调我的):

The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long:

ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;

If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class--serialVersionUID fields are not useful as inherited members. Array classes cannot declare an explicit serialVersionUID, so they always have the default computed value, but the requirement for matching serialVersionUID values is waived for array classes.

关于Java序列化和反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14504338/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com