save up to 40%

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

Rating:

The steps to read text from a file are as follows:

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

    using System.IO;

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

    StreamReader sr = new StreamReader("TestFile.txt");
    or
    StreamReader sr;
    sr = new StreamReader("TestFile.txt");

  3. Create a string object that temporarily stores the text to be read from the file.

    String line;

  4. Insert a while statement to read the text from starting to the end of the file using the ReadLine() method, and display the text.

    While(line=sr.ReadLine()!=NULL)
    {
       Console.WriteLine(line);
    }


  5. Now, close the StreamReader class.

    sr.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.