0

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

I ave a file already made in C:\myfile.txt and it has some data in it.. Now when I try to use File files = new File("C:\myfile.txt"); , It overwrites th orignal data and inserts the new data in it.. Is there a mechanism to aviod overwriting of old data?...

3
  • 1
    Do you want to add data to the file? Consider this question Otherwise what would you like to happen to the old and new files? Commented Nov 24, 2012 at 21:40
  • 1
    Absolutely. How would your code know what to do with the existing data? Commented Nov 24, 2012 at 21:40
  • 1
    Look at this [FileOutputStream Constructor](docs.oracle.com/javase/7/docs/api/java/io/…, boolean)) and it's boolean append parameter. Commented Nov 24, 2012 at 21:40

3 Answers 3

3

You could use append mode in one of the file writer classes:

FileWriter writer = new FileWriter("myfile.txt", true); 
Sign up to request clarification or add additional context in comments.

Comments

3

use the method new FileOutputStream(File,true) to append to an existing file.

Comments

1

Provide true in the constructor to append the file as told above For more detailed control, use RandomAccessFile

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.