我创建了一个ObjectOutputStream
ObjectOutputStream stream = new ObjectOutputStream(new ByteArrayOutputStream());
stream.writeObject(myObject);
但是我现在如何将它转换回Object
,甚至是ByteArray
?
我试过像这样得到一个ObjectInputStream
ByteArrayOutputStream outputStream = (ByteArrayOutputStream) myProcess.getOutputStream();
final ObjectInputStream objectInputStream = new ObjectInputStream(
new ByteArrayInputStream(outputStream.toByteArray()));
但是我得到一个编译错误,说它无法将ObjectOutputStream
强制转换为ByteArrayOutputStream
;然而,ObjectOutputStream
似乎没有方法来获取数据?
在这里你是如何做到的
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream stream = new ObjectOutputStream(baos);
stream.writeObject(myObject);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream inputStream = new ObjectInputStream(bais);
Object o = inputStream.readObject();