BufferedWriter writes over existing file [duplicate]
Asked Answered
S

5

9

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());
    }
}
Supersession answered 31/12, 2013 at 16:47 Comment(5)
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.Uncork
See FileWriter(File,append) & FileWriter(String,append).Ardyth
@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'. ;)Ardyth
@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. 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.Uncork
@HovercraftFullOfEels Couldn't hurt.. :)Ardyth
M
15

What you seek for is Append mode.

new FileWriter(file,true); // true = append, false = overwrite
Maharashtra answered 31/12, 2013 at 16:49 Comment(0)
D
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();
Detrude answered 31/12, 2013 at 16:50 Comment(0)
G
1

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

Gonzalo answered 31/12, 2013 at 16:50 Comment(0)
A
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

Acropetal answered 31/12, 2013 at 16:50 Comment(0)
P
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().

Paapanen answered 31/12, 2013 at 16:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.