I'm trying to append an ArrayList to a binary file so I can save its data when the program is closed. The snippet below shows how I'm writing them
public void writeInFile(String filePath, ArrayList<Dealership> dealershipList, boolean append) {
File file = new File(filePath);
ObjectOutputStream oos = null;
FileOutputStream fos = null;
try {
if (!file.exists() || !append) oos = new ObjectOutputStream (new FileOutputStream (file));
else oos = new AppendableObjectOutputStream (new FileOutputStream (file, append));
oos.writeObject(dealershipList);
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
I'm using the AppendableObjectOutputStream
from this solution. And here is how I'm reading from the file
public ArrayList<Dealership> readDealeshipFromFile(String filePath) {
ArrayList<Dealership> readDealerships = new ArrayList<Dealership>();
try {
FileInputStream fis = new FileInputStream(filePath);
ObjectInputStream ois = new ObjectInputStream(fi);
readDealerships = (ArrayList<Dealership>) or.readObject();
or.close();
fi.close();
} catch (Exception e) {
e.printStackTrace();
}
return readDealerships;
}
All the data I write in the first time is read correctly. But when the new data should be appended, only the first information is returned, and I can't figure out what's causing this.