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

open the file for append, not overwrite.

as far as im aware this opens the file for append:

fos = new FileOutputStream("newdata.dat", true);

it should, but the code you posted can't be the code you're using as it won't ever compile.
Most likely therefore your actual code is different from the code you show here and does something else.

it should, but the code you posted can't be the code you're using as it won't ever compile.
Most likely therefore your actual code is different from the code you show here and does something else.

that is the code i use? i call them functions to write data to the file and it only writes the last one?

This article has been dead for over six months. Start a new discussion instead.