【问题标题】:Object Deserialization- to get back int array from serialized Object对象反序列化 - 从序列化对象中取回 int 数组
【发布时间】:2013-05-01 14:03:14
【问题描述】:
int[] myIntArray;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new DeflaterOutputStream(byteArrayOutputStream));
objectOutputStream.writeObject(myIntArray);

现在,ObjectOutputStream 获取对象并直接序列化它。 DeflaterOutputStream对序列化结果进行压缩,然后将压缩后的结果存放在一个ByteArrayOutputStream

谁能告诉我如何反序列化并取回我原来的 int 数组? 请分享代码?

【问题讨论】:

    标签: java serialization deserialization object-serialization


    【解决方案1】:
    objectOutputStream.close();
    byte[] serialized = byteArrayOutputStream.getBytes();
    
    // and then read back using symmetric constructs as when writing, but using 
    // input streams instead of output streams:
    
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(serialized);
    ObjectInputStream objectInputStream = 
        new ObjectInputStream(new InflaterInputStream(byteArrayInputStream));
    int[] myDesererializedIntArray = (int[]) objectInputStream.readObject();
    

    【讨论】:

    • 您可能需要在转换为字节之前关闭或完成流。我将编辑我的答案。
    • 在这里查看我的完整代码stackoverflow.com/questions/16321507/…
    • 好的。我终于找到了问题所在。您必须使用 InflaterInputSTream 而不是 DeflaterInputStream。很抱歉没有尽快测试我的解决方案。我现在已经测试成功了。
    猜你喜欢
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多