Object Deserialization-从序列化Object获取int数组

时间:2013-05-01 14:03:15

标签: java serialization deserialization object-serialization

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

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

有人能告诉我如何反序列化并取回原来的int数组吗? Plz分享编码吗?

1 个答案:

答案 0 :(得分: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();