Skip to content Skip to sidebar Skip to footer

Write To Txt File, But Not Overwrite

I got a little problem, it seems simple (personally I think it is), but I couldn't find a answer. But atleast I don't know how to fix it. I write some lines to a .txt file when I h

Solution 1:

You have to do

writer_contact = new BufferedWriter(new FileWriter(root + "/Save/Contacten.txt",true));

Just as said in java documentation:

FileWriter

public FileWriter(File file,
              boolean append)
       throws IOException

Constructs a FileWriter object given a File object. If the second argument istrue, then bytes will be written to the endof the file rather than the beginning.

Parameters:
file - a File objectto write to
append - iftrue, then bytes will be written to the endof the file rather than the beginning 

Solution 2:

try:

publicFileWriter(File file, boolean append)

set the append to true

Solution 3:

Well given this is just a little of your code (and I'm assuming you chunked it out so as to not reveal other parts) what I suspect is going on is that you're opening the file root + "/Save/Contacten.txt" in a non-append mode. The first time you call it the file is created and written to. Subsequent times you call it, it finds the file, and recreates (or deletes content) and then writes to it.

Try using:

writer_contact = new BufferedWriter(new FileWriter(root + "/Save/Contacten.txt", true));

Of course the first time you open/create the file you'll want it to be false (unless you ALWAYS want to append if the file already exists).

Give that a spin.

Solution 4:

you can check for if file exits or not ?

or you can also append old file.

If not exits then and then only create new one.

Post a Comment for "Write To Txt File, But Not Overwrite"