Cannot Serialize/Deserialize ArrayList

后端 未结 2 957
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-05 20:11

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


        
相关标签:
2条回答
  • 2021-01-05 20:21

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

    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.

    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.

    0 讨论(0)
  • 2021-01-05 20:33

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

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

    0 讨论(0)
提交回复
热议问题