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?...

Community
  • 1
  • 1
user841852
  • 197
  • 3
  • 7
  • 15
  • 1
    Do you want to add data to the file? Consider [this question](http://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java) Otherwise what would you like to happen to the old and new files? – Mark Meyer Nov 24 '12 at 21:40
  • 1
    Absolutely. How would your code know what to do with the existing data? – darrengorman Nov 24 '12 at 21:40
  • 1
    Look at this [FileOutputStream Constructor](http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html#FileOutputStream(java.io.File, boolean)) and it's `boolean append` parameter. – jlordo Nov 24 '12 at 21:40

3 Answers3

3

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

jtahlborn
  • 51,973
  • 5
  • 73
  • 115
3

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

FileWriter writer = new FileWriter("myfile.txt", true);
Reimeus
  • 155,977
  • 14
  • 207
  • 269
1

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

Jatin
  • 29,900
  • 12
  • 95
  • 152