Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Reading Files Using StreamReader | Data Structures & File Handling
C# Beyond Basics

bookReading Files Using StreamReader

You can open and read files using the C# programming language.

For reading a file, we create a new StreamReader object. The StreamReader object takes in the path of the file.

Note
Note

The term "object" will become more clear in the later sections however for understanding purposes - an object is simply an instance of a data type, and StreamReader is a data type just like int or float. So the values 1, 2.5f and "Hello World" can be technically referred to as "objects" of the int, float and string data types respectively.

index.cs

index.cs

copy
1
StreamReader fileVarName = new StreamReader(fullPath);

Here fileVarName represents the variable name in which you will store the StreamReader object, and fullPath is supposed to be the full path of the file in the form of a string.

For example you can to open a file "text.txt" on Desktop which has the path C:/Users/Admin/Desktop/text.txt:

index.cs

index.cs

copy
1
StreamReader textFile = new StreamReader("C:/Users/Admin/Desktop/text.txt");
Note
Note

You can also use implicit declaration to make the syntax slightly shorter: var textFile = new StreamReader("C:/Users/Admin/Desktop/text.txt");

Consider the text.txt file has the following content:

text.txt

text.txt

copy
12345
This is some example text

A StreamReader object has a ReadLine method which reads one line from the file and returns it:

index.cs

index.cs

copy
1234
var textFile = new StreamReader("C:/Users/Admin/Desktop/text.txt"); string line = textFile.ReadLine(); Console.WriteLine(line);

Output:

This

The ReadLine method automatically switches the cursor to the next line so when it's called again, it reads the next line if there is any, otherwise it simply returns null (nothing):

index.cs

index.cs

copy
12345678
var textFile = new StreamReader("C:/Users/Admin/Desktop/text.txt"); Console.WriteLine(textFile.ReadLine()); // This Console.WriteLine(textFile.ReadLine()); // is Console.WriteLine(textFile.ReadLine()); // some Console.WriteLine(textFile.ReadLine()); // example Console.WriteLine(textFile.ReadLine()); // text Console.WriteLine(textFile.ReadLine()); // NULL

Output:

This
is
some
example
text

For reading all the lines from a file we can use the while loop with a condition which checks if the next line is null or not. Following is a simple way you can do that:

index.cs

index.cs

copy
12345678
var textFile = new StreamReader("C:/Users/Admin/Desktop/text.txt"); string line = textFile.ReadLine(); while(line != null) { Console.WriteLine(line); line = textFile.ReadLine(); }

Output:

This
is
some
example
text

You can make the above code a bit neater using a trick, for that let's take a look at the assignment statements. An assignment statement has a return value as well, which is the value that is being assigned. It can be understood with an example:

index.cs

index.cs

copy
12
int a; Console.WriteLine(a = 7);

Output:

7

It outputs 7 because the statement a = 7 returns 7 which is the value being assigned to a.

You can use this information to do some creative things like assigning the same value to multiple variables in a single statement:

index.cs

index.cs

copy
123456789
int a = 1; int b = 1; int c = 1; a = b = c = 9; Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c);

Output:

9
9
9

Using this information, you can modify the file reading code to make it shorter removing unnecessary code:

index.cs

index.cs

copy
1234567
var textFile = new StreamReader("C:/Users/Admin/Desktop/text.txt"); string line; while((line = textFile.ReadLine()) != null) { Console.WriteLine(line); }

Inside the condition you first used an assignment statement line = textFile.ReadLine() which automatically updates the line variable and then checks if it's null from the statement's return value.

After reading the file you must also close it using the Close method, otherwise it can potentially cause memory leaks or the file can be locked and become inaccessible from other places as long as the program is running.

The final code will look like this:

index.cs

index.cs

copy
123456789
var textFile = new StreamReader("C:/Users/Admin/Desktop/text.txt"); string line; while ((line = textFile.ReadLine()) != null) { Console.WriteLine(line); } textFile.Close();
question-icon
=();

Click or drag`n`drop items and fill in the blanks

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 6

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 2.04

bookReading Files Using StreamReader

Swipe to show menu

You can open and read files using the C# programming language.

For reading a file, we create a new StreamReader object. The StreamReader object takes in the path of the file.

Note
Note

The term "object" will become more clear in the later sections however for understanding purposes - an object is simply an instance of a data type, and StreamReader is a data type just like int or float. So the values 1, 2.5f and "Hello World" can be technically referred to as "objects" of the int, float and string data types respectively.

index.cs

index.cs

copy
1
StreamReader fileVarName = new StreamReader(fullPath);

Here fileVarName represents the variable name in which you will store the StreamReader object, and fullPath is supposed to be the full path of the file in the form of a string.

For example you can to open a file "text.txt" on Desktop which has the path C:/Users/Admin/Desktop/text.txt:

index.cs

index.cs

copy
1
StreamReader textFile = new StreamReader("C:/Users/Admin/Desktop/text.txt");
Note
Note

You can also use implicit declaration to make the syntax slightly shorter: var textFile = new StreamReader("C:/Users/Admin/Desktop/text.txt");

Consider the text.txt file has the following content:

text.txt

text.txt

copy
12345
This is some example text

A StreamReader object has a ReadLine method which reads one line from the file and returns it:

index.cs

index.cs

copy
1234
var textFile = new StreamReader("C:/Users/Admin/Desktop/text.txt"); string line = textFile.ReadLine(); Console.WriteLine(line);

Output:

This

The ReadLine method automatically switches the cursor to the next line so when it's called again, it reads the next line if there is any, otherwise it simply returns null (nothing):

index.cs

index.cs

copy
12345678
var textFile = new StreamReader("C:/Users/Admin/Desktop/text.txt"); Console.WriteLine(textFile.ReadLine()); // This Console.WriteLine(textFile.ReadLine()); // is Console.WriteLine(textFile.ReadLine()); // some Console.WriteLine(textFile.ReadLine()); // example Console.WriteLine(textFile.ReadLine()); // text Console.WriteLine(textFile.ReadLine()); // NULL

Output:

This
is
some
example
text

For reading all the lines from a file we can use the while loop with a condition which checks if the next line is null or not. Following is a simple way you can do that:

index.cs

index.cs

copy
12345678
var textFile = new StreamReader("C:/Users/Admin/Desktop/text.txt"); string line = textFile.ReadLine(); while(line != null) { Console.WriteLine(line); line = textFile.ReadLine(); }

Output:

This
is
some
example
text

You can make the above code a bit neater using a trick, for that let's take a look at the assignment statements. An assignment statement has a return value as well, which is the value that is being assigned. It can be understood with an example:

index.cs

index.cs

copy
12
int a; Console.WriteLine(a = 7);

Output:

7

It outputs 7 because the statement a = 7 returns 7 which is the value being assigned to a.

You can use this information to do some creative things like assigning the same value to multiple variables in a single statement:

index.cs

index.cs

copy
123456789
int a = 1; int b = 1; int c = 1; a = b = c = 9; Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c);

Output:

9
9
9

Using this information, you can modify the file reading code to make it shorter removing unnecessary code:

index.cs

index.cs

copy
1234567
var textFile = new StreamReader("C:/Users/Admin/Desktop/text.txt"); string line; while((line = textFile.ReadLine()) != null) { Console.WriteLine(line); }

Inside the condition you first used an assignment statement line = textFile.ReadLine() which automatically updates the line variable and then checks if it's null from the statement's return value.

After reading the file you must also close it using the Close method, otherwise it can potentially cause memory leaks or the file can be locked and become inaccessible from other places as long as the program is running.

The final code will look like this:

index.cs

index.cs

copy
123456789
var textFile = new StreamReader("C:/Users/Admin/Desktop/text.txt"); string line; while ((line = textFile.ReadLine()) != null) { Console.WriteLine(line); } textFile.Close();
question-icon
=();

Click or drag`n`drop items and fill in the blanks

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 6
some-alt