Java 如何在ObjectInputStream中初始化一些值?(非空)
我用字节数组初始化了Java 如何在ObjectInputStream中初始化一些值?(非空),java,initialization,inputstream,objectinputstream,Java,Initialization,Inputstream,Objectinputstream,我用字节数组初始化了InputStreamReader,然后初始化了ObjectOutputStream并将其传递给它的构造函数。但它显示错误:无效流头。请帮助我们了解如何为ObjectInputStreamObjectStreams提供一些值,因为它有一个非常特定的格式,所以您不能只创建一个字节数组并期望它的格式正确。您可以使用ObjectOutputStream将对象写入字节数组,这将确保格式正确 // Write an object to a ByteArrayOutputStream B
InputStreamReader
,然后初始化了ObjectOutputStream
并将其传递给它的构造函数。但它显示错误:无效流头
。请帮助我们了解如何为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
传递给InputStream的字节数组是有效的序列化java对象吗?您有代码吗?您确定没有混淆ObjectOutputStream和ObjectInputStream吗?你的问题本身不一致。谢谢:)就是这样。