C Sharp Text Reader

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

C# TextReader

C# TextReader class is found in System.IO namespace. It represents a reader that can be used to read text or sequential series of characters.

C# TextReader Example: Read All Data

Let's see the simple example of TextReader class that reads data till the end of file.

 
  1. using System;  
  2. using System.IO;  
  3. namespace TextReaderExample  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             using (TextReader tr = File.OpenText("e:f.txt"))  
  10.             {  
  11.                 Console.WriteLine(tr.ReadToEnd());  
  12.             }  
  13.         }  
  14.     }  
  15. }  

Output:

Hello C#
C# File Handling by JavaTpoint

C# TextReader Example: Read One Line

Let's see the simple example of TextReader class that reads single line from the file.

 
  1. using System;  
  2. using System.IO;  
  3. namespace TextReaderExample  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             using (TextReader tr = File.OpenText("e:f.txt"))  
  10.             {  
  11.                 Console.WriteLine(tr.ReadLine());  
  12.             }  
  13.         }  
  14.     }  
  15. }  

Output:

Hello C#