In this chapter you will learn:
- What is StreamReader Class?
- How to read file using StreamReader Class?
- Programming Examples and Code
What is StreamReader Class?
StreamReader class allows you to read text files easily. Its implementation is easy and it is widely popular among the programmer. However, there are dozens of way to read text file in C# file handling but
StreamReader Class is more popular in list.
Important Points about
- Implements a
TextReaderthat reads characters from a byte stream in a particular encoding.
StreamReaderclass uses UTF-8 Encoding by defaults.
StreamReaderclass is designed for character input in a particular encoding.
- Use this class for reading standard text file.
- By default, it is not thread safe.
How to read file using StreamReader class?
As mentioned above it is very easy to read text file using StreamReader Class. Here I am going to write a program that does following thing:
- Write some data on text file using StreamWriter class and
- Read those data using StreamReader class.
Programming Examples and Code
static void Main(string args)
string file = @"D:\csharpfile.txt";
//Writer data to text file
using (StreamWriter writer = new StreamWriter(file))
writer.WriteLine("This tutorial explains how to use StreamReader Class in C# Programming");
//Reading text file using StreamReader Class
using (StreamReader reader = new StreamReader(file))
In this chapter you learned
StreamReader class with complete programming example. In the next chapter you will learn TextWriter Class in C#.