Cannot Serialize/Deserialize ArrayList

后端 未结 2 976
爱一瞬间的悲伤
爱一瞬间的悲伤 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.

提交回复
热议问题