是不是可以附加到ObjectOutputStream?
我试图附加到对象列表.以下代码段是一个在作业完成时调用的函数.
FileOutputStream fos = new FileOutputStream
(preferences.getAppDataLocation() + "history" , true);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject( new Stuff(stuff) );
out.close();
Run Code Online (Sandbox Code Playgroud)
但是当我尝试阅读它时,我只得到文件中的第一个.然后我明白了java.io.StreamCorruptedException.
阅读我正在使用
FileInputStream fis = new FileInputStream
( preferences.getAppDataLocation() + "history");
ObjectInputStream in = new ObjectInputStream(fis);
try{
while(true)
history.add((Stuff) in.readObject());
}catch( Exception e ) {
System.out.println( e.toString() );
}
Run Code Online (Sandbox Code Playgroud)
我不知道会有多少个物品存在,所以我在读书时没有例外.从谷歌所说的这是不可能的.我想知道是否有人知道方法?
java serialization append objectoutputstream objectinputstream