无法序列化/反序列化 ArrayList [英] Cannot Serialize/Deserialize ArrayList

查看:118
本文介绍了无法序列化/反序列化 ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试序列化和反序列化一个包含对象的数组列表:

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();
}

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

The HairList object is also a serializable object.

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

This code execution is returning the following error:

java.io.InvalidClassException: java.util.ArrayList;本地类不兼容:流 classdesc serialVersionUID = 8664875232659988799,本地类 serialVersionUID = 8683452581122892189

在行 paramDecoded = (ArrayList) obj_in.readObject();

我不知道我做错了什么.能给点建议吗?

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

更新:

已解决:只是使用了本机的 HairBirt 数组而不是 ArrayList 并且它可以工作:

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

HairBirt[] list = new HairBirt[x];

代替

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

感谢大家的帮助.

推荐答案

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

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

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

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.

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

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.

这篇关于无法序列化/反序列化 ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆