I'm saving objects from two array lists to a file and if I restart the application I have called a read method at the startup which will read the data from the file and add them to the array lists
But when reading from the file only the first object is been read and added to the list even when I add several objects to the array list and save to the file when reading only the first object is read
My Method to Read Objects from the File:
void readData() throws IOException{
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("systemData.txt"))) {
doctorList.add((Doctor) in.readObject());
consultations.add((Consultation) in.readObject());
} catch (EOFException ignored){
} catch (IOException | ClassNotFoundException e ) {
e.printStackTrace();
}catch (ClassCastException ignored) {
}
}
My Method to Save Objects to the File:
@Override
public void saveFile() {
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("systemData.txt"))) {
for (Doctor doctor : doctorList) {
out.writeObject(doctor);
System.out.println("Doctor data saved to the file");
}
for (Consultation consultation: consultations){
out.writeObject(consultation);
System.out.println("Consultation data saved to the file");
}
} catch (IOException e) {
e.printStackTrace();
}
}
Lists, each in one go