-3
我已经初始化InputStreamReader
与一个字节数组,然后初始化ObjectOutputStream
传递给它的构造函数。但它显示错误:invalid stream Header
。请帮助如何给ObjectInputStream
一些价值。如何用它的某些值初始化ObjectInputStream? (非空)
我已经初始化InputStreamReader
与一个字节数组,然后初始化ObjectOutputStream
传递给它的构造函数。但它显示错误:invalid stream Header
。请帮助如何给ObjectInputStream
一些价值。如何用它的某些值初始化ObjectInputStream? (非空)
ObjectStreams
有一个非常具体的格式,所以你不能只是创建一个字节数组,并期望它是在正确的格式。您可以使用ObjectOutputStream
将对象写入字节数组,并确保格式正确。
// Write an object to a ByteArrayOutputStream
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(bout);
oout.writeObject(someObject);
oout.close();
// Read the object from the resulting array
ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray()));
oin.readObject(); // Read the object we wrote in
谢谢:)做到了。 – cruck
是否将字节数组传递给InputStream作为有效的序列化java对象? – rodit
你有任何代码? –
您确定您没有将ObjectOutputStream与ObjectInputStream混淆吗?你的问题与本身不一致。 –