gpt4 book ai didi

java - 对象反序列化 - 从序列化对象中获取 int 数组

转载 作者:行者123 更新时间:2023-12-01 14:37:46 25 4
gpt4 key购买 nike

int[] myIntArray;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new DeflaterOutputStream(byteArrayOutputStream));
objectOutputStream.writeObject(myIntArray);

现在,ObjectOutputStream 获取该对象并直接序列化它。 DeflaterOutputStream 压缩序列化结果,然后将压缩结果存储在 ByteArrayOutputStream

有人可以告诉我如何反序列化并取回我原来的 int 数组吗?请分享代码吗?

最佳答案

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

关于java - 对象反序列化 - 从序列化对象中获取 int 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16319587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com