从 ObjectOutputStream 取回对象
Getting Object back from an ObjectOutputStream
我创建了一个 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();
我创建了一个 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();