8

I'm trying to create a program which saves text on a file and then text can be added onto the file. However, every time i try to write to the file, it overwrites it and doesn't write anything. I need it to add whatever information i want it UNDER the rest.

    FileReader input;
    BufferedReader readFile;

    FileWriter output;
    BufferedWriter writeFile;

    try {
    //  input = new FileReader(password_file);
        //readFile = new BufferedReader(input);

        output = new FileWriter(password_file);
        writeFile = new BufferedWriter(output);


        //while ((temp_user= readFile.readLine()) !=null) {
            //temp_pass = readFile.readLine();
        //}

        temp_user = save_prompt.getText();

        temp_pass = final_password;

                                        //Writes to the file
        writeFile.write(temp_user);
        writeFile.newLine();
        writeFile.write(temp_pass);

    }
    catch(IOException e) {
        System.err.println("Error: " + e.getMessage());
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Victor Strandmoe
  • 113
  • 1
  • 2
  • 7
  • 1
    1) Use the other FileWriter constructor, the one that takes a boolean second parameter. The API should be able to help you. 2) You need to work on your Google search skills a bit. – Hovercraft Full Of Eels Dec 31 '13 at 16:49
  • 1
    See [`FileWriter(File,append)`](http://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html#FileWriter%28java.io.File,%20boolean%29) & [`FileWriter(String,append)`](http://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html#FileWriter%28java.lang.String,%20boolean%29). – Andrew Thompson Dec 31 '13 at 16:50
  • 1
    @HovercraftFullOfEels *"You need to work on your Google search skills a bit."* I have a theory that the OP's search was thwarted because they used 'override' instead of 'overwrite'. But that is just a theory. Don't expect me to explain the (now 5) answers that rushed headlong into answering this without searching. My best guess would be 'vote-slutting'. ;) – Andrew Thompson Dec 31 '13 at 16:58
  • 1
    @AndrewThompson: nah. Just do a Google search using his very own question topic and it brings many results all with the correct answer. The first hit is to another one of many [similar stackoverflow questions](http://stackoverflow.com/questions/4614227/how-to-add-a-new-line-of-text-to-an-existing-file-in-java). This same question has been asked and answered a kagillion times, and one more of the same question on this site will not help anyone in the future. I stand by my recommendation that he work a bit more on his Google skills. – Hovercraft Full Of Eels Dec 31 '13 at 17:07
  • @HovercraftFullOfEels Couldn't hurt.. :) – Andrew Thompson Dec 31 '13 at 17:09

5 Answers5

15

What you seek for is Append mode.

new FileWriter(file,true); // true = append, false = overwrite
PTwr
  • 1,225
  • 1
  • 11
  • 16
8

Replace all existing content with new content.

new FileWriter(file);

Keep the existing content and append the new content in the end of the file.

new FileWriter(file,true);

Example:

    FileWriter fileWritter = new FileWriter(file.getName(),true);
        BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
        bufferWritter.write(data);
        bufferWritter.close();
Salih Erikci
  • 5,076
  • 12
  • 39
  • 69
1

To append the stuff at the end of the file, use the append() method of FileWriter

Sibbo
  • 3,796
  • 2
  • 23
  • 41
1

change the FileWrite liner to:

output = new FileWriter(password_file, true);

which tells FileWriter to append

http://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html

imandrewd
  • 343
  • 1
  • 8
0

Whenever you type

new BufferedWriter(output);

or "write", you are overwriting the "output" file. Try to make sure you only declare a new BufferedWriter once throughout the course of the program, and append() to the file instead of write().

La-comadreja
  • 5,627
  • 11
  • 36
  • 64