aspose file tools*
The moose likes I/O and Streams and the fly likes How do i open a file in append mode for writing? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How do i open a file in append mode for writing?" Watch "How do i open a file in append mode for writing?" New topic
Author

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

Jyothsna Sri Y
Ranch Hand

Joined: Sep 25, 2003
Posts: 35
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

Joined: May 08, 2001
Posts: 1932
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

Joined: Sep 25, 2003
Posts: 35
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

Joined: May 08, 2001
Posts: 1932
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do i open a file in append mode for writing?