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

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

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

4

1 回答 1

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();
于 2013-05-01T14:08:27.580 回答