How to append text to a file?

Are you preparing for IT certification? With practice questions, study notes, interactive quizzes, tips and technical articles, uCertify PrepKits ensure that you get a solid grasp of core technical concepts to ace your certification exam in first attempt.

How to append text to a file?

Rating:

The steps to append text to a file are as follows:

  1. Implement the System.IO namespace using the following code.

    using System.IO;

  2. Create a StreamWriter class object and initialize it with a file name.

    StreamWriter sw = File.AppendText("TestFile.txt");
    or
    StreamWriter sw;
    sw = File.AppendText("TestFile.txt");

  3. Append text to the file using Write() or WriteLine() method of StreamWriter class.

    sw.Write("This is my first Line to the file");
    sw.Write("This is my second Line to the file");
    sw.Write("This is my third line to the file");
    sw.WriteLine("This is my fourth line to the file");

  4. Close the StreamWriter class.

    sw.Close();


Rating:



Other articles

Click here to Article home

Microsoft Certification MCSE: MCSA , MCTS, MCDST, MCAD, MCDBA, MCSE Messaging, MCSE Security
JAVA Certification: SCJP, SCWCD Cisco Certification: CCNA, CCENT, A+, Network+, Security+
Oracle Certification: OCP 9i, OCP 10g, OCA 9i, OCA 10g CIW foundation    Photoshop ACE
© 2008 uCertify.com. All rights reserved. All trademarks are the property of their respective owners.
 
HACKER SAFE certified sites prevent over 99.9% of hacker crime.