Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do i open a file in append mode for writing?

 
Jyothsna Sri Y
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Can someone tell me how to open a text file in append mode for writing to it? I've used buffered writer to write to the file. But I'm used two such streams in different classes to write to the same file. So it appears that the file is being overwritten by the second. is it possible to open in append mode to avoid this...
/*This is in the main*/
BufferedWriter bw= new BufferedWriter(new FileWriter("temp.txt");
bw.write(data);
bw.close();
/*In another class*/
BufferedWriter br= new BufferedWriter(new FileWriter("temp.txt");
br.write(newdata);
br.close();

Thanks.
cheers
Jyothsna.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy Jyothsna!
We don't have many rules at the Ranch, but we do have one regarding initials as last names. I'm a bit culturally inexperienced, so Sri might be your last name, but as it is displayed Y is. Please click on the profile link and change this to conform with our own Internet culture. Thanks!
As for your question, create the FileWriter as follows:
BufferedWriter writer = new BufferedWriter( new FileWriter( "temp.txt" , true ) );
That extra boolean at the end puts it in append mode. The FileWriter API shows the different constructors and their uses.
Now if these two classes are trying to write at the same time, that's a different story. Is that the case?
 
Jyothsna Sri Y
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Jason,
Thanks a lot...It did work...
And regarding my name, thanks for the information, but just wanted to let u know that my last initial is Y, which stand for Yeleswarapu(my surname) and Sri is part of my first name....Sri Jyothsna. Didn't get to create an account as SriJyothsna Y so it's Jyothsna Sri Y, instead...
cheers
Jyothsna.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm glad you got that working.
As far as the last name, perhaps Yeles would do? Not having an initial for a last name is something we enforce, to keep a bit more professional a site. Last names are very rarely, if ever, initialized. So if you could still please comply with that rule, it would be appreciated!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic