2017-02-20 60 views
-3

我已经初始化InputStreamReader与一个字节数组,然后初始化ObjectOutputStream传递给它的构造函数。但它显示错误:invalid stream Header。请帮助如何给ObjectInputStream一些价值。如何用它的某些值初始化ObjectInputStream? (非空)

+0

是否将字节数组传递给InputStream作为有效的序列化java对象? – rodit

+1

你有任何代码? –

+0

您确定您没有将ObjectOutputStream与ObjectInputStream混淆吗?你的问题与本身不一致。 –

回答

0

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 
+0

谢谢:)做到了。 – cruck