Click here to Skip to main content
12,956,606 members (71,261 online)
Rate this:
 
Please Sign up or sign in to vote.
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 14-Jan-13 3:53am
Coder93479
Comments
JasonMacD 14-Jan-13 13:35pm
   
You do realize you posted this under C# right?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
ridoy 14-Jan-13 11:59am
   
+5
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi,

Try this:
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:
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.
  Permalink  
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?
ProgramFOX 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)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,869
CHill60 3,550
Maciej Los 3,302
ppolymorphe 2,080
Jochen Arndt 1,973


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 14 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100