save up to 40%

How to write 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 write text to a file?

Rating:

The steps to write 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 = new StreamWriter("TestFile.txt");
    or
    StreamWriter sw;
    sw = new StreamWriter("TestFile.txt");

  3. Add 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. Add arbitrary objects, if required.

    sw.WriteLine(DateTime.Now);

  5. Now, close the StreamWriter class.

    sw.close();


Rating:



Other articles

Click here to Article home

 
uCertify.com | Our Company | Articles | Privacy | Security | Contact Us | News and Press Release | uCertify India
MCSE: MCSA, MCTS, MCITP    JAVA Certification: SCJP, SCWCD Cisco Certification: CCNA, CCENT, A+, Network+, Security+
Oracle Certification: OCP 9i, OCP 10g, OCA 9i, OCA 10g CIW foundation    EC-212-32    CISSP    Photoshop ACE    Adobe Flash ACE
© 2008 uCertify.com. All rights reserved. All trademarks are the property of their respective owners.