C# StreamReader Code Examples

C# StreamReader is used to read characters to a stream in a specified encoding. Learn how to use C# stream reader. C# StreamReader code example.

C# StreamReader is used to read characters to a stream in a specified encoding. StreamReader.Read method reads the next character or next set of characters from the input stream. StreamReader is inherited from TextReader that provides methods to read a character, block, line, or all content.
 
StreamReader is defined in the System.IO namespace. StreamReader provides the following methods: 
  1. Peak – Returns if there is a character or not.
  2. Read - Reads the next character or next set of characters from the input stream.
  3. ReadAsync - Reads a specified maximum number of characters from the current stream asynchronously and writes the data to a buffer, beginning at the specified index.
  4. ReadBlock - Reads a specified maximum number of characters from the current stream and writes the data to a buffer, beginning at the specified index.
  5. ReadBlockAsync - Reads a specified maximum number of characters from the current stream asynchronously and writes the data to a buffer, beginning at the specified index.
  6. ReadLine - Reads a line of characters from the current stream and returns the data as a string.
  7. ReadLineAsync - Reads a line of characters asynchronously from the current stream and returns the data as a string.
  8. ReadToEnd - Reads all characters from the current position to the end of the stream.
  9. ReadToEndAsync - Reads all characters from the current position to the end of the stream asynchronously and returns them as one string.
 

Creating a StreamReader from a Filename

 
StreamReader constructor takes a full file name or a FileStream object with Encoding, and buffer size as optional parameters.
 
The following code snippet creates a StreamReader from a filename with default encoding and buffer size. 
  1. // File name  
  2. string fileName = @"C:\Temp\CSharpAuthors.txt";  
  3. StreamReader reader = new StreamReader(fileName)  
The following code example creates a StreamReader and reads a file content one line at a time and displays to the console. If there is an exception, the exception is displayed on the console.
  1. using System;  
  2. using System.IO;  
  3. using System.Text;  
  4. class Program  
  5. {  
  6. static void Main(string[] args)  
  7. {  
  8. // File name  
  9. string fileName = @"C:\Temp\CSharpAuthors.txt";  
  10. try  
  11. {  
  12. // Create a StreamReader  
  13. using (StreamReader reader = new StreamReader(fileName))  
  14. {  
  15. string line;  
  16. // Read line by line  
  17. while ((line = reader.ReadLine()) != null)  
  18. {  
  19. Console.WriteLine(line);  
  20. }  
  21. }  
  22. }  
  23. catch (Exception exp)  
  24. {  
  25. Console.WriteLine(exp.Message);  
  26. }  
  27. Console.ReadKey();  
  28. }  
  29. }  
In the above example, the file name is the complete path including a file on a Universal Naming Convention (UNC) share. The default encoding is UTF8Encoding and the buffer size to 1024 bytes.
 

Creating a StreamReader using FileInfo.OpenText

 
StreamReader can be created using the FileInfo.OpenText() method. The following code snippet creates a StreamReader using the FileInfo.OpenText() method. 
  1. FileInfo fi = new FileInfo(fileName);  
  2. StreamReader sr = fi.OpenText();  
  3. string s = "";  
  4. while ((s = sr.ReadLine()) != null) {  
  5. Console.WriteLine(s);  
  6. }  
FileInfo class is useful with file operations. Learn more about FileInfo class here: FileInfo in C#
 

Creating a StreamReader using a FileStream

 
StreamReader constructor takes a FileStream object with Encoding, and buffer size as optional parameters. The following code snippet creates a StreamReader from a FileStream with default encoding and buffer size.
  1. // Create a StreamReader from a FileStream  
  2. using (StreamReader reader = new StreamReader(new FileStream(fileName, FileMode.Open)))  
  3. {  
  4. string line;  
  5. // Read line by line  
  6. while ((line = reader.ReadLine()) != null)  
  7. {  
  8. Console.WriteLine(line);  
  9. }  
  10. }  
The following code snippet creates a StreamReader from a FileStream with Encoding. Encoding supported are ASCII, Unicode, UTF32, UTF7, and UTF8. Encoding is defined in the System.Text namespace.
  1. using (StreamReader reader = new StreamReader(  
  2. new FileStream(fileName, FileMode.Open), Encoding.UTF8))  
The following code snippet creates a StreamReader from a FileStream with encoding and buffer size. 
  1. using (StreamReader reader = new StreamReader(  
  2. new FileStream(fileName, FileMode.Open), Encoding.UTF8, true, 1024))  
Learn more about Encoding in C#, visit Encoding/Decoding in C#. 
 

StreamReader.Peak() method

 
Stream.Reader.Peek() method checks if there are more characters are left to read in a file. The method returns an integer. If the value is less than 0, there are no more characters left to read. The following code snippet uses the Peak() method to check if there are more characters left to read in a file.
  1. // Create a StreamReader  
  2. using (StreamReader reader = new StreamReader(new FileStream(fileName, FileMode.Open)))  
  3. {  
  4. while (reader.Peek() >= 0)  
  5. {  
  6. // Read here  
  7. }  
  8. }  

StreamReader.ReadBlock() method

 
StreamReader.Read() method reads a specified maximum number of characters from the current stream and writes the data to a buffer, beginning at the specified index. The position of the underlying stream is advanced by the number of characters that were read into buffer. The method blocks until either count characters are read, or the end of the stream has been reached. This is a blocking version of Read.
StreamReader.ReadBlockAsync() method reads a specified maximum number of characters from the current stream asynchronously and writes the data to a buffer, beginning at the specified index. 
 

StreamReader.Read() method

 
StreamReader.Read() method reads the next char or next set of chars from the input stream. StreamReader.ReadAsync() method reads the next char or next set of chars from the input stream asynchronously.
 
The following code snippet reads a stream using the Read() method. 
  1. // Create a StreamReader  
  2. using (StreamReader reader = new StreamReader(new FileStream(fileName, FileMode.Open)))  
  3. {  
  4. while (reader.Peek() >= 0)  
  5. {  
  6. Console.WriteLine((char)reader.Read());  
  7. }  
  8. }  

StreamReader.ReadLine() method

 
StreamReader.ReadLine() method reads a line of characters from the current stream and returns the data as a string.
 
StreamReader.ReadLineAsync() method reads a line of characters from the current stream asynchronously and returns the data as a string.
 
The following code snippet reads a file using the ReadLine() method. 
  1. // Create a StreamReader from a FileStream  
  2. using (StreamReader reader = new StreamReader(new FileStream(fileName, FileMode.Open)))  
  3. {  
  4. string line;  
  5. // Read line by line  
  6. while ((line = reader.ReadLine()) != null)  
  7. {  
  8. Console.WriteLine(line);  
  9. }  
  10. }  

StreamReader.ReadToEnd() method

 
StreamReader.ReadToEnd() method reads all characters from the current position to the end of the stream.
 
StreamReader.ReadToEndAsync() method reads all characters from the current position to the end of the stream.
 
The following code snippet reads entire file using the ReadToEnd() method. 
  1. using (StreamReader reader = new StreamReader(fileName))  
  2. {  
  3. Console.WriteLine(reader.ReadToEnd());  
  4. }  

StreamReader Code Example

 
Here is the complete code example of use of StreamReader and its methods. The code also reads the content of the file and displays on the console. 
  1. using System;  
  2. using System.IO;  
  3. using System.Text;  
  4. class Program  
  5. {  
  6. static void Main(string[] args)  
  7. {  
  8. // File name  
  9. string fileName = @"C:\Temp\CSharpAuthors.txt";  
  10. try  
  11. {  
  12. using (StreamReader reader = new StreamReader(fileName))  
  13. {  
  14. Console.WriteLine(reader.ReadToEnd());  
  15. }  
  16. catch (Exception exp)  
  17. {  
  18. Console.WriteLine(exp.Message);  
  19. }  
  20. Console.ReadKey();  
  21. }  
  22. }  
The output of the above code looks like the following:
 
C# StreamReader 

Summary

 
In this article and code sample, we saw how to use a StreamReader class to read a text file.