1

Possible Duplicate:
How to append text to an existing file in Java

I want to add data to a text file. So it's one after another...so something like:

1 2 3 <add more here> 

But I don't want the text from the file to be deleted at all. This is the code i'm using atm, but it replaces what ever is in the file. Could someone please tell me how to do what I asked. Thanks.

 FileWriter fstream = new FileWriter("thefile.txt"); BufferedWriter out = new BufferedWriter(fstream); out.write("blabla"); out.close(); }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } } 
1

1 Answer 1

6

use this

FileWriter fstream = new FileWriter("thefile.txt",true); 

the explanation

public FileWriter(String fileName, boolean append) throws IOException 

Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.

Sign up to request clarification or add additional context in comments.

1 Comment

That fixed the deleting problem thanks. I was able to do the new line thing as well just by doing out.write("\n" + "blabla");

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.