2017-02-20 41 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