Trying to make a Program that writes and reads Multiple Serialized Objects to a file as necessary(random access or even serial).I have appended multiple objects to my file but cant load more than one, i get IOException. The writes happen multiple times before trying to read so it does not find EOF.
Object Class
public class Account implements Serializable {
private static final long serialVersionUID = 1L;
private String firstName;
private String lastName;
private String age;
private String telephone;
private String program;
private String gender;
private double balance;
Write
try {
FileOutputStream fileOut = new FileOutputStream("Database.dat", true);
ObjectOutputStream DatabaseOut = new ObjectOutputStream(fileOut);
DatabaseOut.writeObject(AccToWrite);
DatabaseOut.close();
fileOut.close();
//dbg
JOptionPane.showMessageDialog(null, "Account Was Registerd", "Registration", JOptionPane.INFORMATION_MESSAGE);
}
Read
try {
FileInputStream fis = new FileInputStream("Database.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
Account tempaccount = (Account) ois.readObject();
System.out.println(tempaccount.toString());
Account tempaccount2 = (Account) ois.readObject();
System.out.println(tempaccount2.toString());
fis.close();
ois.close();
} catch (FileNotFoundException x) {
System.out.println("File not found");
} catch (IOException x) {
System.out.println("Error initializing stream");
}
Any help is much appreciated.