everytime i try to append a new object to file it just over rights the whole file this is the code im using to add a new object to the file:
public void add1() {
FileOutputStream fos = null;
{
ObjectOutputStream out = null;
try {
fos = new FileOutputStream("newdata.dat", true);
out = new ObjectOutputStream(fos);
HealthProblem[] healthproblem = new HealthProblem[4];
healthproblem[0] = new BrainProblem("Test1", "Test1", "Notes", "Test1");
healthproblem[1] = new BrainProblem("Test2", "Test2", "Notes", "Test2");
healthproblem[2] = new BrainProblem("Test3", "Test3", "Notes", "Test3");
healthproblem[3] = new LungProblem("test1", "test2", "Notes", "test3", "test4");
patient1 = new Patient();
patient1.setId(1);
patient1.setName("Luke Houlahan");
patient1.setAddress("100 test");
patient1.setTelNumber("01484710204");
patient1.setHealthProblem(healthproblem);
out.writeObject(patient1);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
//Close the ObjectOutputStream
try {
if (out != null) {
out.flush();
out.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
public void add2() {
FileOutputStream fos = null;
{
ObjectOutputStream out = null;
try {
fos = new FileOutputStream("newdata.dat", true);
out = new ObjectOutputStream(fos);
HealthProblem[] healthproblem = new HealthProblem[2];
healthproblem[0] = new BrainProblem("Test1", "Test1", "Notes", "Test1");
healthproblem[1] = new LungProblem("test1", "test2", "Notes", "test3", "test4");
Patient patient = new Patient();
patient.setId(2);
patient.setName("just a test");
patient.setAddress("100 fff");
patient.setTelNumber("014847fffff10204");
patient.setHealthProblem(healthproblem);
out.writeObject(patient1);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
//Close the ObjectOutputStream
try {
if (out != null) {
out.flush();
out.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
all i get is the last entry :/ ive looked everywhere for a solution but no look :/ if they is no solution then i suppose ill have to use mySQL or something