gpt4 book ai didi

java - 实现 Serialized 的类无法读取Object

转载 作者:行者123 更新时间:2023-12-02 06:36:50 25 4
gpt4 key购买 nike

我正在尝试将 Pair 序列化为以下内容:

private void writeObject(java.io.ObjectOutputStream out) throws IOException {
if (mPair != null) {
String first = mPair.first;
String second = mPair.second;

mPair = null;

try {
out.writeChars(first);
out.writeChars("\n");
out.writeChars(second);
} catch (Exception e) {
}
}
}

private void readObject(java.io.ObjectInputStream in) throws IOException,
ClassNotFoundException {
try {
String first = in.readLine();
String second = in.readLine();

mPair = new Pair<String, String>(first, second);
} catch (EOFException e) {
mPair = new Pair<String, String>("", "");
}
}

当我的应用程序离开屏幕时,我调试了 writeObject 被正确调用,我有 3 个自定义类对象,但是当我返回应用程序时 readObject 从未被调用.

最佳答案

事实证明,这个简单的解决方案似乎有效:

public class SerializableStringPair extends Pair<String, String> implements
Serializable {

private static final long serialVersionUID = 1L;

public SerializableStringPair(String first, String second) {
super(first, second);
}
}

关于java - 实现 Serialized 的类无法读取Object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19565581/

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