This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How do i open a file in append mode for writing?
 
Similar Threads
Writing to File
Trouble appending new data to an existing file
HELP!!! Output stream closing prematurely
Opening files in append mode
Writing data into file from a String variable