File Handling Concept In C#

Now, we will learn writing/reading into/through the file, using StreamWriter / StreamReader classes

Let us start,

First of all, it is important to note that

  1. We need to use FileInfo class functionality, which is provided through System.IO namespace.
  2. We need to use StreamWriter class functionality for the purpose of writing into the file, which is provided through System.IO namespace.
  3. We need to use StreamReader class functionality for the purpose of reading from the file, which is provided through System.IO namespace.

Let’s go through the code snippets, 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.IO;  
  6. //Writing/Reading into/through file Through streamWriter/ StreamReader  
  7. //sandip patil File handling Demo App  
  8. namespace FileHandling {  
  9.     class Program {  
  10.         static void Main(string[] args) {  
  11.             FileInfo file = new FileInfo("E:\\myFile.txt");  
  12.             StreamWriter sw = file.AppendText();  
  13.             sw.WriteLine("Welcome contents::");  
  14.             for (int i = 0; i < 10; i++) {  
  15.                 sw.WriteLine("Content:{0} ", +i);  
  16.             }  
  17.             sw.Close();  
  18.             StreamReader sr = file.OpenText();  
  19.             string input = null;  
  20.             while ((input = sr.ReadLine()) != null) {  
  21.                 Console.WriteLine(input);  
  22.             }  
  23.             sr.Close();  
  24.             Console.Read();  
  25.         }  
  26.     }  
  27. }   

Output of above program is=>

Welcome contents::
Content:0
Content:1
Content:2
Content:3
Content:4
Content:5
Content:6
Content:7
Content:8
Content:9

Explanation

The program given above will create myFile.txt on E:\\ drive of the users system, if it’s already not existing on the user's system

The line of code given below demonstrates it.

  1. FileInfo file = new FileInfo("E:\\myFile.txt");   

Let’s see how to write into the file.

The line of code given below demonstrates it. 

  1. StreamWriter sw = file.AppendText();  
  2. sw.WriteLine("Welcome contents::");  
  3. for (int i = 0; i < 10; i++) {  
  4.     sw.WriteLine("Content:{0} ", +i);  
  5. }   

It appends the text contents in your myFile.txt File

The line of code given below is used to close StreamWriter instance. Once writing operation into the file is over, proceed to how to read from the file.

  1. sw.Close();   

Let’s see how to read from the file.

The line of code given below demonstrates it. 

  1. StreamReader sr = file.OpenText();  
  2. string input = null;  
  3. while ((input = sr.ReadLine()) != null) {  
  4.     Console.WriteLine(input);  
  5. }   

The line of code given below is used to close StreamReader instance, once reading operation from the file is over.

  1. sr.Close();   

OpenText() function is used to open the file for the reading purpose.

Now, in order to read the line by line text contents, we can use ReadLine() function, which is mentioned above.