java 无法序列化/反序列化 ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/678481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:21:36  来源:igfitidea点击:

Cannot Serialize/Deserialize ArrayList

javaserialization

提问by sakana

I'm trying to serialize and deserialize an array list with a object inside:

我正在尝试使用内部对象序列化和反序列化数组列表:

HairBirt param = new HairBirt();
param.setName("name");
param.setValue(2.3f);

HairBirt param2 = new HairBirt();
param2.setName("name2");
param2.setValue(2.4f);

ArrayList<HairBirt> list = new ArrayList<HairBirt>();

list.add(param);

list.add(param2);

ByteArrayOutputStream bos = null;
try {
    bos = new ByteArrayOutputStream();
    ObjectOutputStream obj_out = new ObjectOutputStream(bos);
    obj_out.writeObject(list);
} catch (IOException e) {
    e.printStackTrace();
}

String encoded = bos.toString();
try {
    encoded = URLEncoder.encode(encoded, "UTF-8");
} catch (UnsupportedEncodingException e1) {
    e1.printStackTrace();
}
System.out.print("The serialized output is: " + encoded);   

//DECODE

ArrayList<HairBirt> paramDecoded;

String myParam = null;
try {
    myParam = URLDecoder.decode(encoded, "UTF-8");
} catch (UnsupportedEncodingException e1) {
    e1.printStackTrace();
}
System.out.println("Got parameters");
ByteArrayInputStream bis = new ByteArrayInputStream(myParam.getBytes());

try {
    ObjectInputStream obj_in = new ObjectInputStream(bis);

    paramDecoded = (ArrayList<HairBirt>) obj_in.readObject();
} catch (IOException e) {
    e.printStackTrace();
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}

The HairList object is also a serializable object.

HairList 对象也是一个可序列化的对象。

This code execution is returning the following error:

此代码执行返回以下错误:

java.io.InvalidClassException: java.util.ArrayList; local class incompatible: stream classdesc serialVersionUID = 8664875232659988799, local class serialVersionUID = 8683452581122892189

java.io.InvalidClassException: java.util.ArrayList; local class incompatible: stream classdesc serialVersionUID = 8664875232659988799, local class serialVersionUID = 8683452581122892189

in line paramDecoded = (ArrayList<HairBirt>) obj_in.readObject();

排队 paramDecoded = (ArrayList<HairBirt>) obj_in.readObject();

I don't know what i'm doing wrong. Can you give a tip?

我不知道我做错了什么。你能给个提示吗?

Update:

更新:

Resolved:Just used a native array of HairBirt instead of a ArrayList and it works:

已解决:只需使用本机的 HairBirt 数组而不是 ArrayList,它就可以工作:

HairBirt[] list = new HairBirt[x];

instead of

代替

ArrayList<HairBirt> list = new ArrayList<HairBirt>();

Thank you all for your help.

谢谢大家的帮助。

回答by Jon Skeet

Don't use ByteArrayOutputStream.toString()- instead, use toByteArray()and base64-encode that binary data to convert it into a string without losing information.

不要使用ByteArrayOutputStream.toString()- 而是使用toByteArray()并对该二进制数据进行 base64 编码以将其转换为字符串而不会丢失信息。

I strongly suspect that's the main problem - that you were losing the data after serialization. You should probably also close or at least flush the ObjectOutputStream. I don't know offhand whether that actually does anything in this case, but it would seem to be a good idea.

我强烈怀疑这是主要问题 - 您在序列化后丢失了数据。您可能还应该关闭或至少刷新ObjectOutputStream. 我不知道在这种情况下这是否真的有任何作用,但这似乎是一个好主意。

I don't believe there's any base64 support directly in Java (in a public class, anyway) but there are various 3rd party libraries you can use, such as the one in the Apache Commons Codec library.

我不相信 Java 中直接有任何 base64 支持(无论如何在公共类中),但是您可以使用各种 3rd 方库,例如Apache Commons Codec 库中的库

回答by Steve B.

Have you tried overriding this behavior by declaring your own serialVersionUID in your custom class?

您是否尝试通过在自定义类中声明自己的 serialVersionUID 来覆盖此行为?

Do you have a specific reason for doing the extra step of serializing through a string? Normally you would just deserialize through an ObjectOutputStream.

您是否有特定原因执行通过字符串进行序列化的额外步骤?通常,您只需通过 ObjectOutputStream 进行反序列化。