我正在尝试序列化和反序列化包含对象的数组列表:
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对象也是一个可序列化的对象。
java.io.InvalidClassException: java.util.ArrayList; local class incompatible: stream classdesc serialVersionUID = 8664875232659988799, local class serialVersionUID = 8683452581122892189
一致
paramDecoded = (ArrayList<HairBirt>) obj_in.readObject();
我不知道我做错了什么。你能给我小费吗?
更新:
断然的:
HairBirt[] list = new HairBirt[x];
而不是
ArrayList<HairBirt> list = new ArrayList<HairBirt>();
谢谢大家的帮助。