gpt4 book ai didi

java - 尝试保存和读取文件时出现 java.io.InvalidClassException

转载 作者:行者123 更新时间:2023-11-30 08:58:48 25 4
gpt4 key购买 nike

public static void save(Settings settings) {
try {
ObjectOutputStream os =
new ObjectOutputStream(
new FileOutputStream(settingsFile));
os.writeObject(settings);
os.close();
} catch(Exception e) {
e.printStackTrace();
}
}

这是将类设置保存到文件的代码。

public static Settings load() {
try {
ObjectInputStream is =
new ObjectInputStream(
new FileInputStream(settingsFile));
Settings settings = (Settings) is.readObject();
is.close();
return settings;
} catch(Exception e) {
e.printStackTrace();
}
return null;
}

此代码将读取文件并返回转换为设置的对象

错误是get在

Settings settings = (Settings) is.readObject();    

StackTrace 是

java.io.InvalidClassException: com.Settings; local class incompatible: stream classdesc serialVersionUID = 5215625451621355973, local class serialVersionUID = -6263468811172060812
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 com.Settings.load(Settings.java:51)
at com.Main.main(Main.java:23)

我不完全确定为什么会收到此错误,因为写入文件的对象是设置,而正在读取的对象也是设置。任何帮助将不胜感激

最佳答案

您序列化的类的版本为 X,您当前使用的 Settings 类的版本为 Y(其中 versions 是版本每个 Serializable 都有的 UID,即使你没有定义一个,它也会由 Java 通过查看你的类内部生成)

将来在您创建可序列化的类中定义一个私有(private)的(无论如何,它也可以是公共(public)的/ protected )static final long serialVersionUID,带有一个值(例如1L) 将避免这个问题。

要修复您的异常,现在定义一个 serialVersionUID,其值为 5215625451621355973(因此它们将匹配)并反序列化它。 (而且不安全。)

无论如何,我应该用新的 UID 值(1L?)重新序列化它以使其始终兼容,因为如果您不提供此值,您可能会鼓励其他人出错。

Serializable JavaDoc 比我解释得更好。

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 - 尝试保存和读取文件时出现 java.io.InvalidClassException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27489780/

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