Click here to Skip to main content
15,891,837 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how can i write string to a file , an update the file through my program?
(I want to write at the end of the text file each time)
Posted
Comments
JasonMacD 14-Jan-13 13:35pm    
You do realize you posted this under C# right?

 
Share this answer
 
Comments
ridoy 14-Jan-13 11:59am    
+5
Hi,

Try this:
Java
FileWriter writer = new FileWriter("file.txt");
writer.write("Some text to append...");
writer.close();

If a file contains "This is text.", and if you execute this code, then your text file will contain this:
This is text.Some text to append...

If you want a new line after "This is text", then use the escape sequence for a new line:
Java
FileWriter writer = new FileWriter("file.txt");
writer.write("\nSome text to append...");
writer.close();

In that case, your text file will contain this:
This is text.
Some text to append...

Hope this helps.
 
Share this answer
 
v2
Comments
Coder93 14-Jan-13 14:41pm    
If sometime I want to write at the end of the file and sometime i want to delete the
previous file and create a new one with same name what should i do?
Thomas Daniels 15-Jan-13 12:31pm    
If you want to clear a file, and then writing, have a look here:
http://answers.yahoo.com/question/index?qid=20060906014353AAQBCn9

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900