我将ArrayLists写入文件。我用FileInputStream读取它。但总是只是通过阅读出现“第一个”ArrayList。我尝试使用readInt()/ wirteInt()和循环,但总是抛出异常,通过调用readInt() - > EOF 我想将此File中的所有ArrayList读入ArrayList。我的应用程序需要持久化,所以我序列化了ArrayLists。
写入文件:
try {
FileOutputStream fos = new FileOutputStream(_cache, true);
ObjectOutputStream os = new ObjectOutputStream(fos);
// os.writeInt(newValueList.size()); // Save size first
os.writeObject(newValueList);
os.flush();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FileOutputStream fos = new FileOutputStream(_cache, true);
ObjectOutputStream os = new ObjectOutputStream(fos);
// os.writeInt(newValueList.size()); // Save size first
os.writeObject(newValueList);
os.flush();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
从文件中读取:
答案 0 :(得分:1)
如果您多次编写相同的列表,它只会写一次列表。之后它将使用相同的引用,但内容将是相同的(任何对象都是如此)
如果你在writeObject()之间调用reset()
,它每次都会发送一份新的列表副本。