It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How to write a string in a file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How to write a string in a file?" Watch "How to write a string in a file?" New topic
Author

How to write a string in a file?

Ananda Kashyap
Greenhorn

Joined: Jan 03, 2001
Posts: 23
Hi,
I am trying to write "Hello World!" in a file name x.txt. Previously the file contained "I am Ananda". I want Hello World to be in the exact next line. How do I do that? I am using RAF and writeUTF but it doesn't go to next line and put a "?" before Hello. I know about writeUTF, why it's writing the ? but wondering if there any other way to do this.
Please help.
- Ananda.
Tom Pridham
Ranch Hand

Joined: Feb 06, 2001
Posts: 92
You need to seek() to the end of the file, then write a "\r\n", then write the string you wish to append to the file. When you open the text file in Notepad, the new next will be on a new line.
Ananda Kashyap
Greenhorn

Joined: Jan 03, 2001
Posts: 23
Tom,
Thanks for the advice but which function should I use? I used writeUTF(String s) for "Hello World!" but it puts a "?" before Hello.. and what about "\r\n". I am trying with writeChar('\r') as well as writeChars("\r\n"); don't work.
- Ananda.
Addy Ed
Greenhorn

Joined: Feb 22, 2001
Posts: 2
Hi
Lets see if this helps:
{
FileOutputStream f= new FileOutputStream("c:\\x.txt" , true);
/*Note: append is true*/
PrintStream p= new PrintStream(f);
p.println();
p.println("Hey its Workin");
}

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to write a string in a file?