Basics of File Handling in C#

09 Sep, 2019
Generally, the file is used to store the data. The term File Handling refers to the various operations like creating the file, reading from the file, writing to the file, appending the file, etc. There are two basic operation which is mostly used in file handling is reading and writing of the file. The file becomes stream when we open the file for writing and reading. A stream is a sequence of bytes which is used for communication. Two stream can be formed from file one is input stream which is used to read the file and another is output stream is used to write in the file. In C#, System.IO namespace contains classes which handle input and output streams and provide information about file and directory structure.
File-Handling-Class-Hierarchy
Here we are going to discuss about two classes which are useful for writing in and reading from the text file.
The StreamWriter class implements TextWriter for writing character to stream in a particular format. The class contains the following method which are mostly used.
Example:
// C# program to write user input // to a file using StreamWriter Classusing System;using System.IO;  namespace GeeksforGeeks {      class GFG {          class WriteToFile {                  public void Data()        {            // This will create a file named sample.txt            // at the specified location             StreamWriter sw = new StreamWriter("H://geeksforgeeks.txt");                          // To write on the console screen            Console.WriteLine("Enter the Text that you want to write on File");                           // To read the input from the user            string str = Console.ReadLine();                           // To write a line in buffer            sw.WriteLine(str);                           // To write in output stream            sw.Flush();                           // To close the stream            sw.Close();         }    }          // Main Method    static void Main(string[] args)    {        WriteToFile wr = new WriteToFile();        wr.Data();        Console.ReadKey();    }}}
Input:
Output: You will find the file at the specified location having the content:
The StreamReader class implements TextReader for reading character from the stream in a particular format. The class contains the following method which are mostly used.
Example:
// C# program to read from a file// using StreamReader Classusing System;using System.IO;  namespace GeeksforGeeks {      class GFG {          class ReadFile {                  public void DataReading()        {            // Takinga a new input stream i.e.             // geeksforgeeks.txt and opens it            StreamReader sr = new StreamReader("H://geeksforgeeks.txt");                          Console.WriteLine("Content of the File");                           // This is use to specify from where             // to start reading input stream            sr.BaseStream.Seek(0, SeekOrigin.Begin);                          // To read line from input stream            string str = sr.ReadLine();                           // To read the whole file line by line            while (str != null)             {                Console.WriteLine(str);                str = sr.ReadLine();            }            Console.ReadLine();                           // to close the stream            sr.Close();         }    }          // Main Method    static void Main(string[] args)    {        ReadFile wr = new ReadFile();        wr.DataReading();    }}}
Output:
File Handling
C#
File Handling
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# | Multiple inheritance using interfaces
Introduction to .NET Framework
C# | Delegates
Differences Between .NET Core and .NET Framework
C# | Data Types
C# | Method Overriding
C# | String.IndexOf( ) Method | Set - 1
C# | Class and Object
C# | Constructors
C# | Arrays