gpt4 book ai didi

java - 当我尝试反序列化对象时出错

转载 作者:行者123 更新时间:2023-12-02 08:52:05 25 4
gpt4 key购买 nike

我和 friend 一起使用 GitHub 进行编码,当我们更新主类时,IDE 向我们提供了以下错误:

Exception in thread "main" java.io.InvalidClassException: Packages.contoCorrente; local class incompatible: stream classdesc serialVersionUID = -433050874178120905, local class serialVersionUID = -5213999116280532749
at java.base/java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:689)
at java.base/java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1958)
at java.base/java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1827)
at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2115)
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1646)
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:464)
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:422)
at java.base/java.util.ArrayList.readObject(ArrayList.java:928)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.base/java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1160)
at java.base/java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2271)
at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2142)
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1646)
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:464)
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:422)
at ObjectWrite.main(ObjectWrite.java:31)

为了解决这个问题,我总是删除主对象文件,这样它就可以重建它。我还附上了处理反序列化的部分代码,希望有人能帮助我。

public static void main(String[] args) throws Exception {

ArrayList<contoCorrente> contiCorrentiArray;
File file = new File("contiCorrenti");
if (file.exists()) {
FileInputStream fileIn = new FileInputStream(file);
ObjectInputStream fileObjIn = new ObjectInputStream(fileIn);

//noinspection unchecked
contiCorrentiArray = (ArrayList<contoCorrente>) fileObjIn.readObject();


fileObjIn.close();
fileIn.close();
} else {
contiCorrentiArray = new ArrayList<>();
}

System.out.println("Numero Conti Presenti: " + contiCorrentiArray.size() + "\n");

最佳答案

您和您的合作伙伴是否具有相同的serialVersionUID?

如果没有,你们都应该定义它,这样它就不会自动计算。

来自文档:

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/60706519/

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