Functions of C# StreamReader
We use streams for reading data from the text file in C# programming. The additional layer between the application and the file is stream.
The stream gets the data that has been broken down from the larger files into smaller bits. The application does not have to read all of the data from the larger file because it can read small bits of data from the streams.
Here, we elaborate on different examples in which we use the StreamReader concept. We are performing these examples in Ubuntu 20.04 using the text editor of Ubuntu 20.04.
Example # 1
In this example, we use the “StreamReader” class which reads the data present in the text file. For this, you must have a text file so that you will read the data of this text file using the “C# StreamReader”. Here, we have a text file named “csharp.txt” which is saved in the home of Ubuntu 20.04 and we want to read the data of this file. Now, look at the image below in which we are reading the data from this file.
The first line of code contains the statement “using System”. As it is a library for providing the functions and classes which we are going to use in this program. In the next line, we write “using System.IO” which is used for input\output, and “using System.Linq” which is a namespace that gives classes and methods supporting the LINQ queries. Then, we have namespace “System.Threading.Tasks” for creating tasks and getting objects from the class by using the task class.
We import the namespace named “DemoProgram”. We invoked a class here named “Example”. Inside this class, we have called the “main” method. We kept this “main” function static and (String  args) is here for the representation of arguments. Inside this function, we are going to declare the “string” with the name “path” and give the path of that text file whose data we want to read. Here, we want to read the data of our file named “csharp.txt” and it is saved in the home of Ubuntu 20.04. This code is also saved in the same location, so we just type the filename whose data will be read.
If you want to read the data of those files which are in another directory, you must give the directory name along with the filename. After this, we are creating the object of the “StreamReader” with the name “strm” here and we are going to pass a parameter named “path” to File.OpenText. For the opening of the file, we use “File.OpenText”. Next, we declared a variable with the name “S” which is temporary. We are using this variable “S” for the reading of data.
After that, the “while” loop is used. In this loop, “strm.ReadLine()” will read the character of the file and store the line one by one in the “S” variable and if it’s not null, then “Console.WriteLine(S)” will print this line on the terminal. This loop works until the given condition is true. After that, we have “Console.ReadKey”, which is used to wait until the key is pressed.
For getting the output, we must use two commands. One is “mcs” and the second one is the “mono” command. In the image, you can see that when we use the “mcs” command, we use the filename with the “.cs” extension. When we are using the “mono” command, we use the “.exe” extension with the filename.
You can see here that it prints the data of the file whose path is given in the C# code. The “StreamReader” reads all the data and after reading this, we print all data of the file with the help of “Console.WriteLine”.
Example # 2:
Now, we are going to demonstrate another example here in which we are reading another text file using “StreamReader”.
In this code, first, we have “using System” which is compulsory in our C# program. This is a library for accessing methods and functions. The “using” is a keyword that is used for taking the parameters. When we use any IDisposable object, we use this “using” keyword. For data handling, we have the namespace “System.IO”. It is used for input and output. The statement “using System.Linq” is there for making our code more readable.
Then for classes, abstract base and helper classes, we use the “System.Text” statement as it contains all these classes. Then, we have “System.Threading.Task” for creating tasks. We can also access the objects of that task. After that, the namespace is named “CSharp”. Then, we are going to invoke the “main” function. We are declaring the “string” variable with the name “path” and giving the path of the file to this string variable. The path of the file is “names.txt”. You must put “@” before the path of the file.
We are creating the object of “FileStream” with the name “fs” which represents our file “names.txt”. Then, we create the constructor with the same name “FileStream” and pass parameters to this constructor. First, we pass the path of the file as a parameter to this constructo,r and then pass “FileMode.Open” which is used for opening the file. This file is saved in our system and we want to read this file.
So, for this, we are going to open this file by using “FileMode.Open”. The last parameter is “FileAccess.Read” here. We are using this to access and read the file. We write this statement in the “using” keyword, so we don’t need to close this file. Now, we are creating another object named “reader” of the “StreamReader” class using the keyword “new” and then creating a constructor of that class with the same name and passing the parameter “fs” to this constructor. The “fs” is the “stream”. We save our file in the “fs” stream so we pass the “fs” stream here as a parameter.
Whenever we create the object of the “StreamReader” class, we must close this. We used the “using” keyword with this as well instead of closing the file. Now, we are going to create an array of name “names” and the length of this array is “5”, which means we can store 5 names in this array and we get these 5 names from the file whose data we want to read.
With the help of a loop, we are going to store data in this array. We use the “for” loop here. This “loop” is executed until the condition is true, and the condition here is that the loop executes until it is equal to the “array length”. The value of “a” increments each time the “loop” executes. In the next line, we pass “a” to our array “names” and with the reader in which our file is present, we call a method “ReadLine()”. We access all the data which is stored in an array with the help of the “foreach” loop. Then, we read data from this array and print it on the screen using the “Console.WriteLine” statement.
The given output in the image shows that it reads the data of the file and prints the first 5 lines of the file.
In this tutorial, we have described the concept of the C# StreamReader in detail and also elaborated on different examples in which we have used different codes for reading the data, and then printing the data of that file on the screen. We have explained both codes in detail and try our best to clear your concepts of “StreamReader”. You can read data of any text file using this “StreamReader” method. But remember that you must give the correct path of the file. If your file is saved in another directory, you must mention that directory in the path of that file.