我有以下方法:
public static void writeContestantsToFile(ArrayList<Contestant> contestants) throws IOException {
FileOutputStream fos = new FileOutputStream("minos.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(contestants);
oos.flush();
oos.close();
}
但是我想知道是否可以使用另一个数组列表再次写入文件 minos.dat ?例如有另一种方法是这样的:
public static void writeContestantsToFile(ArrayList<Times> times) throws IOException {
FileOutputStream fos = new FileOutputStream("minos.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(times);
oos.flush();
oos.close();
}
我能够检索时间数组列表和参赛者吗?或者我需要写入单独的文件吗?
最佳答案
可以,但如果您不想覆盖文件的内容,而只想追加,请像这样打开它:
FileOutputStream fos = new FileOutputStream("minos.dat", true);
(使用this constructor)
关于java - 是否可以通过不同的方法写入同一个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6587394/