且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法序列化/反序列化 ArrayList

更新时间:2022-02-22 18:00:26

不要使用 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.