Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

bufferedWriter/fileWriter overwrites instead of appending

807605Jul 15 2007 — edited Jul 15 2007
hi all,

i want to write data to a file but it seems that if i have old data in the file,
the new data will overwrite it instead of appending it

here's my code, please help! thank you!

...
try {
BigInteger sentResult = getSendEmailPort().sendEmail(fromName, fromEmail, toName, toEmail, subject, msgBody, acknowledgement, priority);
int result = sentResult.intValue();
String fileName = "C:/Documents and Settings/user/Desktop/myproject/bdata.txt" ;
if (result == 0)
{
String message = "Thank you for referring " + toName + " to us!";
request.setAttribute("resultMessage",message);

FileWriter writer = new FileWriter( fileName, true );
writer.write(savedData);
writer.close();
...

i've tried this method as well:

BufferedWriter writer = new BufferedWriter(new FileWriter("C:/Documents and Settings/user/Desktop/myproject/bdata.txt"));
writer.write(savedData);
writer.newLine();
writer.flush();
writer.close();

and this (this one does append but the appended data looks like this "t h i s i s t h e d a t a a p p e n d e d", all the spaces in between:

File file= new File("C:/Documents and Settings/user/Desktop/myproject/bdata.txt");
RandomAccessFile raf = new RandomAccessFile(file,"rw");
raf.seek(file.length());
raf.writeChars(savedData);
raf.close();
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 12 2007
Added on Jul 15 2007
6 comments
788 views