0 java file append arraylist object
我正在尝试在 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();
}
Run Code Online (Sandbox Code Playgroud)
我现在的问题是,如果我想保存相同的 ArrayList 但使用另一个数据,或者如果我重新打开文件,文件中的第一个数据将被覆盖。
有人能告诉如何将这些新数据附加到文件中吗?
谢谢你。
append
在 FileOutputStream ctor中将标志设置为 true。
FileOutputStream ofs = new FileOutputStream(file, true);
Run Code Online (Sandbox Code Playgroud)
看
http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html