C#
14 BEST C# Books (2021 Update)
C-SHARP (C#) is a general-purpose, multi-paradigm programming language developed by Microsoft that runs...
In C# file operations, normally streams are used to read and write to files. A stream is an additional layer created between an application and a file. The stream is used to ensure smooth read and write operations to the file.
Streams are normally used when reading data from large files. By using streams, the data from large files in broken down into small chunks and sent to the stream. These chunks of data can then be read from the application.
The reason for breaking it down into small chunks is because of the performance impact of reading a big file in one shot. If you were to read the data from say, a 100 MB file at one shot, your application could just hang and become unstable. The best approach is then to use streams to break the file down into manageable chunks.
So when a write operation is carried out on file, the data to be written, is first written to the stream. From the stream, the data is then written to the file. The same goes for the read operation. In the read operation, data is first transferred from the file to the stream. The data is then read from the application via the stream. Let's look at an example of how we can read and write using streams.
In this tutorial, you will learn-
The stream reader is used to read data from a file using streams. The data from the file is first read into the stream. Thereafter the application reads the data from the stream.
For our example, we will assume that we have a file in the D drive called Example.txt. The file will be a simple text file and have 2 lines as shown below
For our example, we will create a simple Console application and work with File streams
Let's look at an example of how we can use streams for reading data from a file. Enter the below code in the program.cs file.
using System; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DemoApplication { class Tutorial { static void Main(string[] args) { String path = @"D:\Example.txt"; using (StreamReader sr = File.OpenText(path)) { String s = ""; while ((s = sr.ReadLine()) != null) { Console.WriteLine(s); } } Console.ReadKey(); } } }
Code Explanation:-
When the above code is set, and the project is run using Visual Studio, you will get the below output.
Output:-
From the output, you can see that the Stream Reader read both the lines from the file. Finally, the lines of the string read from the stream were sent to the Console.
The stream writer is used to write data to a file using streams. The data from the application is first written into the stream. After that the stream writes the data to the file. Let's look at an example of how we can use streams for writing data from a file. Enter the below code in the program.cs file.
using System; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DemoApplication { class Tutorial { static void Main(string[] args) { String path = @"D:\Example.txt"; using (StreamWriter sr = File.AppendText(path)) { sr.WriteLine("Guru99 - ASP.Net"); sr.Close(); Console.WriteLine(File.ReadAllText(path)); } Console.ReadKey(); } } }
Code Explanation:-
When the above code is set, and the project is run using Visual Studio, you will get the below output.
Output:-
From the output, you can see that the line "Guru99 – ASP.Net" was added to the file successfully. All the 3 lines of text can be seen in the console.
Summary
C-SHARP (C#) is a general-purpose, multi-paradigm programming language developed by Microsoft that runs...
What is .Net Framework? .Net Framework is a software development platform developed by Microsoft for...
In this tutorial, you will learn- .Net Framework Version History C# Version History .Net Framework...
What is Queue in C#? The Queue is a special case collection which represents a first in first out...
A typical software automation Testing requires automation tool like Selenium and QTP. Coded UI is...
C# is one of the languages provided by Microsoft to work with .Net. This language encompasses a...