【发布时间】:2015-10-15 16:20:56
【问题描述】:
我正在尝试在 Java 上做一个应用程序,我需要将 ArrayLists 保存在一个文件中。我一直在寻找如何做到这一点,我找到了下一个代码:
ArrayList<Expediente> al = new ArrayList<Expediente>();
Expediente exp = new Expediente("Expediente data");
try{
File file = new File("Pathname");
FileOutputStream ofs = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(ofs);
oos.writeObject(al);
oos.close();
ofs.close();
} catch (IOException e) {
System.err.println("Error!");
e.printStackTrace();
}
我现在的问题是,如果我想保存相同的 ArrayList 但包含另一个数据,或者如果我重新打开文件,文件中的第一个数据将被覆盖。
有人能告诉我如何在文件中附加这些新数据吗?
谢谢。
【问题讨论】:
-
您看过 Java API 文档吗?
-
我认为这是重复的。那里有很好的答案。 stackoverflow.com/questions/1625234/…
-
是的,我知道 FileWriter 和 PrintWriter,但我需要编写对象,而不是字符串。不过还是谢谢你:)
标签: java file object arraylist append