我想在ObjectInputStream中放置一个对象。我编写了以下代码将对象写入ObjectOuputStream并将其保存在" myObjectFile" 中。然后阅读" myObjectFile" 以获取ObjectInputStream:
val oos = new ObjectOutputStream(new FileOutputStream("myObjectFile"))
oos.writeObject(myObject);
oos.close();
val ois = new ObjectInputStream(new FileInputStream("myObjectFile"))
:
ois.close();
是否可以直接为myObject创建一个ObjectInputStream而无需编写中间" myObjectFile"?
我也尝试了以下内容:
val baos = new ByteArrayOutputStream
val oos = new ObjectOutputStream(baos)
oos.writeObject(myObject);
oos.flush();
oos.close();
val is = new ByteArrayInputStream(baos.toByteArray())
val ois = new ObjectInputStream(is)
println(" baos size = " + baos.size())
println(" ois size = " + IOUtils.toByteArray(ois).length)
但我得到了:
baos size = 369
ois size = 0
为什么ois大小为0?我做错了什么?谢谢!