StreamReader And StreamWriter Classes In C#

This article explains C# StreamReader and StreamWriter classes and how to use them.

StreamReader and StreamWriter are found in the System.IO namespace. Both classes are useful when you want to read or write character-based data. Both of these classes deal with Unicode characters.
 
StreamReader derives from the Abstract class "TextReader" and StreamWriter derives from "TextWriter".

The following table shows some members of the StreamWriter class.
 
Member Description
Close() Closes the current StreamWriter object and the underlying stream. This method is equivalent to Dispose(), that is used to release resources.
Write()  This method is used to write data to a text stream without a newline. 
WriteLine() This method is used to write data to a text stream with a newline.
 
The following table shows some members of the StreamReader class.
 
Member Description
Close() Closes the current StreamReader object and the underlying stream. This method is equivalent to Dispose(), that is used to release resources.
Read() Reads the next character from the input stream.
ReadLine() Reads a line of characters from the current stream and returns the data as a string.
ReadToEnd() Reads the stream from the current position to the end of the stream.
 
Let's look at an example, where we write data to a file using StreamWriter and the read data from the file using StreamReader. To read from or write to a file, the File class provides 2 methods that return a StreamReader or a StreamWriter object respectively, in other words:
  • CreateText(string filepath): To write data to a file; returns a StreamWriter object
  • OpenText(string filepath): To read data from file; returns a StreamReader object
The following describes how to create a sample.
 
Create a new project named “StreamWriterAndStreamReaderExample” and create 2 static methods, one to read a file and another to write to a file. In other words ReadFromFile() and WriteToFile(). Import the System.IO namespace in your program's source file that contains all the classes to deal with file input and output.
 
In this example, we'll write tables from 1 to 10 in a file and will read that file using these classes.

Program.cs
  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             Console.ForegroundColor = ConsoleColor.Green;  
  6.      Console.Title = "StreamReader and STreamWriter";  
  7.             WriteToFile();  
  8.             ReadFromFile();  
  9.             Console.ForegroundColor = ConsoleColor.Gray;  
  10.         }  
  11.   
  12.         public static void ReadFromFile()  
  13.         {  
  14.             using (StreamReader sr = File.OpenText(@"E:\Programming Practice\CSharp\Console\table.tbl"))  
  15.             {  
  16.                 string tables = null;  
  17.                   
  18.                 while ((tables = sr.ReadLine()) != null)  
  19.                 {  
  20.                     Console.WriteLine("{0}",tables);  
  21.                 }  
  22.                 Console.WriteLine("Table Printed.");  
  23.             }  
  24.         }  
  25.   
  26.         public static void WriteToFile()  
  27.         {  
  28.             using (StreamWriter sw = File.CreateText(@"E:\Programming Practice\CSharp\Console\table.tbl"))  
  29.             {  
  30.                 sw.WriteLine("Please find the below generated table of 1 to 10");  
  31.                 sw.WriteLine("");  
  32.                 for (int i = 1; i <= 10; i++)  
  33.                 {  
  34.                     for (int j = 1; j <= 10; j++)  
  35.                     {  
  36.                         sw.WriteLine("{0}x{1}= {2}",i,j,(i*j));  
  37.                     }  
  38.                     sw.WriteLine("==============");  
  39.                 }  
  40.                 Console.WriteLine("Table successfully written on file.");  
  41.             }  
  42.         }  
  43.     }  
In the preceding example, the WriteToFile() method is used to write data to the file “table.tbl” and then the ReadFromFile() method is used to read data from that file. You can create the file with any extension, like .txt, .abc, .tlb, and so on. To write data to the file, we created a StreamWriter object that calls the WriteLine() method to write the data and to read the data from the file, we created a StreamReader object, that calls the ReadLine() method and while reading the file, the data is stored in a string in a while loop until it reads all the data. Call both of the methods in Main().
 
When you run your application, WriteToFile() will create a file at the specified location as shown below:
Call both the methods in Main
Open this file in Notepad to view the content.
Open this file in Notepad
Output
 
output

I hope this article helps you in understanding the StreamReader and StreamWriter classes in C#.
 
If there is any mistake in this article then please let me know. Please provide your valuable feedback and comments, that enables me to provide a better article the next time.
 
Here are two detailed tutorials on StreamReader and StreamWriter,