10X Sale
kh logo
All Courses
  1. Tutorials
  2. Programming Tutorials

File Handling in C#

Updated on Sep 3, 2025
 
46,005 Views

File Handling involves management of files. Files are a collection of data that is stored in a disk with a name and a directory path. Files contain input as well as output streams that are used for reading and writing data respectively.

The System.IO Namespace

The System.IO Namespace has various classes and types that allow reading of files and writing to files. Some of the classes in this namespace are given as follows:

Classes

Description

FileStream

This class provides a Stream for a file, supporting both synchronous and asynchronous read and write operations.

StreamReader

This class implements a TextReader that reads characters from a byte stream in a particular encoding.

StreamWriter

This class implements a TextWriter for writing characters to a stream in a particular encoding.

TextReader

This class represents a reader that can read a sequential series of characters.

TextWriter

This class represents a writer that can write a sequential series of characters. This class is abstract.

BinaryReader

This class reads primitive data types as binary values in a specific encoding.

BinaryWriter

This class writes primitive types in binary to a stream and supports writing strings in a specific encoding.

StringReader

This class implements a TextReader that reads from a string.

StringWriter

This class implements a TextWriter for writing information to a string. The information is stored in an underlying StringBuilder.

FileInfo

This class provides properties and instance methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects. This class cannot be inherited.

DirectoryInfo

This class exposes instance methods for creating, moving, and enumerating through directories and subdirectories. This class cannot be inherited.

Source: MSDN

The examples using various classes in the System.IO namespace are given as follows:

FileStream Class

This class provides a Stream for a files. It supports both synchronous as well as asynchronous read and write operations.

A program that demonstrates the FileStream Class is as follows:

using System;  
using System.IO;  
namespace FileHandlingDemo
{
  public class Example
 {
   public static void Main(string[] args)  
   {
       FileStream fi = new FileStream("e:\\file.txt", FileMode.OpenOrCreate);
       fi.WriteByte(12);
       fi.WriteByte(6);
       fi.WriteByte(30);
       fi.Close();  
       FileStream fo = new FileStream("e:\\file.txt", FileMode.OpenOrCreate);
       int i = 0;  
       Console.WriteLine("The contents of the file are:");
       while ((i = fo.ReadByte()) != -1)  
       {
           Console.WriteLine(i);  
       }
       fo.Close();  
   }
 }
}

The output of the above program is as follows:

The contents of the file are:
12
6
30

StreamReader and StreamWriter Class

The StreamReader Class reads the characters from a byte stream using a TextReader. The Read() and ReadLine() methods are used to read the data from the stream. The StreamWriter Class writes the characters to a byte stream.

A program that demonstrates the StreamReader and StreamWriter Class is as follows:

using System;  
using System.IO;  
namespace FileHandlingDemo
{
  public class Example  
  {
   public static void Main(string[] args)  
   {
       FileStream f1 = new FileStream("e:\\file.txt", FileMode.OpenOrCreate);  
       StreamWriter s1 = new StreamWriter(f1);  
       s1.WriteLine("File Handling in C#");  
       s1.Close();  
       f1.Close();  
       FileStream f2 = new FileStream("e:\\file.txt", FileMode.OpenOrCreate);  
       StreamReader s2 = new StreamReader(f2);  
       string data = s2.ReadLine();  
       Console.WriteLine("The data in the file is as follows:");  
       Console.WriteLine(data);  
       s2.Close();  
       f2.Close();  
   }
 }
}

Source Code: Program to implement StreamReader and StreamWriter Class in C#

The output of the above program is as follows:

The data in the file is as follows:
File Handling in C#

TextReader Class and TextWriter Class

The TextReader Class has a reader that reads the characters from the file while the TextWriter Class has a writer that writes the characters into the file.

A program that demonstrates the TextReader and TextWriter Class is as follows:

using System;  
using System.IO;  
namespace FileHandlingDemo
{  
   class Example
   {
       static void Main(string[] args)  
       {
           using (TextWriter tw = File.CreateText("e:\\file.txt"))  
           {
               tw.WriteLine("C# File Handling");
               tw.WriteLine("TextReader Class and TextWriter Class");  
           }
           using (TextReader tr = File.OpenText("e:\\file.txt"))  
           {
               Console.WriteLine(tr.ReadToEnd());  
           }
       }
   }
}

Source Code: Program to implement TextReader and TextWriter Class in C#

The output of the above program is as follows:

C# File Handling
TextReader Class and TextWriter Class

BinaryReader Class and BinaryWriter Class

The BinaryReader Class reads the data types in the form of binary information and the BinaryWriter Class writes the binary information into the stream.

A program that demonstrates the BinaryReader and BinaryWriter Class is as follows:

using System;  
using System.IO;  
namespace FileHandlingDemo
{  
   class Example
   {
       static void Main(string[] args)  
       {
           string fName = "e:\\file.dat";  
           using (BinaryWriter bw = new BinaryWriter(File.Open(fName, FileMode.Create)))  
           {
                bw.Write("File Handling in C#");  
                bw.Write("BinaryReader class and BinaryWriter class");
           }
       using (BinaryReader br = new BinaryReader(File.Open("e:\\file.dat", FileMode.Open)))  
           {
               Console.WriteLine(br.ReadString());  
               Console.WriteLine(br.ReadString());  
           }
       }
   }
}

Source Code: Program to implement BinaryReader and BinaryWriter Class in C#

The output of the above program is as follows:

File Handling in C#
BinaryReader class and BinaryWriter class

StringReader Class and StringWriter Class

The StringReader Class reads the data from the string using TextReader. Similarly, the StringWriter Class writes the data to the string using the TextWriter.

A program that demonstrates the StringReader and StringWriter Class is as follows:

using System;  
using System.IO;  
namespace FileHandlingDemo
{  
   class Example
   {
       static void Main(string[] args)  
       {
           StringWriter sw = new StringWriter();  
           sw.WriteLine("File Handling in C#");  
           sw.WriteLine("StringReader Class and StringWriter Class");
           sw.Close();
           StringReader sr = new StringReader(sw.ToString());  
           while (sr.Peek() > -1)  
           {
               Console.WriteLine(sr.ReadLine());  
           }
       }
   }
}

Source Code: Program to implement StringReader and StringWriter Class in C#

The output of the above program is as follows:

File Handling in C#
StringReader Class and StringWriter Class
+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Get your free handbook for CSM!!
Recommended Courses