Close
Close

File Handling in C#


When a program runs, the data is in the memory but when it ends or the computer shuts down, it gets lost. To keep data permanently, we need to write it in a file.

File is used to store data. In this topic, you will learn about reading data from a file and writing data to the file.

We use FileStream class of C# to read and write on files which is present in System.IO. So, we need to include it before using it i.e., using System.IO;

We create an object of FileStream class to read and write on files.

FileStream f = new FileStream(fileName, Mode);

fileName is the name of the file.

We need to tell the computer the purpose of opening our file. For e.g.- to write on the file, to read from the file, etc. These are the different modes in which we can open a file.

FileMode Description
Append Opens a text file for appending (appending means to start adding text from the end of the file). If the file doesn't exist, it will create a new one.
Create Creates a new file and if the file already exists overwrite it.
CreateNew Creates a new file but if the file already exist, throws an exception.
Open It opens an existing file.
OpenOrCreate If file exists opens a file, otherwise creates a new one.
Truncate It opens an existing file and truncates its size to zero bytes.
In Append, the cursor will be at the end of the file but in modes like Open, OpenOrCreate, the cursor will be at the start even if there is already a text in the file.

So, to open a file file.txt in current directory, we would write:

FileStream f = new FileStream("file.txt", FileMode.OpenOrCreate);

To close a file, we use Close method. So, to close the above opened file we would write - f.Close().

using System;
using System.IO;

class Test
{
  static void Main(string[] args)
  {
    FileStream f = new FileStream("file.txt", FileMode.OpenOrCreate);
    f.Close();
  }
}
Output

C# WriteByte


We can use the WriteByte method to write a single byte in a file.

using System;
using System.IO;

class Test
{
  static void Main(string[] args)
  {
    FileStream f = new FileStream("file.txt", FileMode.OpenOrCreate);
    f.WriteByte(97);
    f.Close();
  }
}
Output

StreamWriter and StreamReader in C#


StreamWriter class is used to write character-based data in a file and StreamReader is used to read character-based data from a file.

We create an object of StreamWriter and pass the object of FileStream while creating it. For example, we will pass the object f of FileStream class created above to make an object of StreamWriter i.e., StreamWriter s = new StreamWriter(f);.

using System;
using System.IO;

class Test
{
  static void Main(string[] args)
  {
    FileStream f = new FileStream("file.txt", FileMode.OpenOrCreate);
    StreamWriter s = new StreamWriter(f);
    s.WriteLine("CodesDope");
    s.Close();
    f.Close();
  }
}
Output

Similarly, we will create an object of StreamReader.

using System;
using System.IO;

class Test
{
  static void Main(string[] args)
  {
    FileStream f = new FileStream("file.txt", FileMode.OpenOrCreate);
    StreamReader s = new StreamReader(f);
    string s1 = s.ReadLine();
    Console.WriteLine(s1);
    s.Close();
    f.Close();
  }
}
Output
CodesDope

TextWriter and TextReader in C#


TextWriter is an abstract class. StreamWriter is derived from this class. We use it also to handle character-based data.

using System;
using System.IO;

class Test
{
  static void Main(string[] args)
  {
    using(TextWriter writer = File.CreateText("file.txt"))
    {
      writer.WriteLine("Hello");
      writer.WriteLine("CodesDope");
    }
  }
}
Output
using System;
using System.IO;

class Test
{
  static void Main(string[] args)
  {
    using(TextReader reader = File.OpenText("file.txt"))
    {
      Console.WriteLine(reader.ReadToEnd());
    }
  }
}
Output
Hello
CodesDope

You can see official documentation for more.


Ask Yours
Post Yours
Doubt? Ask question