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 returns 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
- class Program
- {
- static void Main(string[] args)
- {
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Title = "StreamReader and STreamWriter";
- WriteToFile();
- ReadFromFile();
- Console.ForegroundColor = ConsoleColor.Gray;
- }
-
- public static void ReadFromFile()
- {
- using (StreamReader sr = File.OpenText(@"E:\Programming Practice\CSharp\Console\table.tbl"))
- {
- string tables = null;
-
- while ((tables = sr.ReadLine()) != null)
- {
- Console.WriteLine("{0}",tables);
- }
- Console.WriteLine("Table Printed.");
- }
- }
-
- public static void WriteToFile()
- {
- using (StreamWriter sw = File.CreateText(@"E:\Programming Practice\CSharp\Console\table.tbl"))
- {
- sw.WriteLine("Please find the below generated table of 1 to 10");
- sw.WriteLine("");
- for (int i = 1; i <= 10; i++)
- {
- for (int j = 1; j <= 10; j++)
- {
- sw.WriteLine("{0}x{1}= {2}",i,j,(i*j));
- }
- sw.WriteLine("==============");
- }
- Console.WriteLine("Table successfully written on file.");
- }
- }
- }
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:
Open this file in Notepad to view the content.
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 enable me to provide a better article the next time.