Java file writing with append rather than overwriting?

107 Views Asked by At

I have this code that creates a file and saves the users input, but it keeps overwriting and I want it to save each entry a user gives. How can I do this?

    File file = new File("info.txt");
    BufferedWriter output = null;
    try {
        output = new BufferedWriter(new FileWriter(file));
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        output.write("Users pick:  " + myint+ "\t");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        output.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //file writer
1

There are 1 best solutions below

1
On

Change

output = new BufferedWriter(new FileWriter(file));

to

output = new BufferedWriter(new FileWriter(file,true));

for opening the file in append mode.