File Handling In C#

In this article, you'll learn how to use files and folders related classes availalbe in .NET using C#.
 

Stream

 
When you open a file for reading or writing, it becomes stream. Stream is a sequence of bytes traveling from a source to a destination over a communication path.
 
The two basic streams are input and output streams. Input stream is used to read and output stream is used to write.
 
The System.IO namespace includes various classes for file handling.
 
The parent class of file processing is stream. Stream is an abstract class, which is used as the parent of the classes that actually implement the necessary operations.
 
The primary support of a file as an object is provided by a .NET Framework class called File. This static class is equipped with various types of (static) methods to create, save, open, copy, move, delete, or check the existence of a file.
 

Diagram to represent file-handling class hierarchy 

 
file_handling.gif 
 
Note
FileIno, DirectoryInfo and DriveInfo classes have instance methods. File, Directory, Path classes have static methods.
 
The following table describes some commonly used classes in the System.IO namespace.
 
Class Name
Description

FileStream

It is used to read from and write to any location within a file
BinaryReader
It is used to read primitive data types from a binary stream
BinaryWriter
It is used to write primitive data types in binary format
StreamReader
It is used to read characters from a byte Stream
StreamWriter
It is used to write characters to a stream.
StringReader
It is used to read from a string buffer
StringWriter
It is used to write into a string buffer
DirectoryInfo
It is used to perform operations on directories
FileInfo
It is used to perform operations on files
 

Reading and writing in the text file

 
StreamWriter Class
 
The StreamWriter class in inherited from the abstract class TextWriter. The TextWriter class represents a writer, which can write a series of characters.
 
The following table describes some of the methods used by StreamWriter class.
 
Methods
Description
Close
Closes the current StreamWriter object and the underlying stream

Flush

Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream

Write

Writes to the stream

WriteLine

Writes data specified by the overloaded parameters, followed by end of line
 
Program to write user input to a file using StreamWriter Class
  1. using System;  
  2. using System.Text;  
  3. using System.IO;  
  4. namespace FileWriting_SW  
  5. {  
  6.     class Program  
  7.     {  
  8.         class FileWrite  
  9.         {  
  10.             public void WriteData()  
  11.             {  
  12.                 FileStream fs = new FileStream("c:\\test.txt", FileMode.Append, FileAccess.Write);  
  13.                 StreamWriter sw = new StreamWriter(fs);  
  14.                 Console.WriteLine("Enter the text which you want to write to the file");  
  15.                 string str = Console.ReadLine();  
  16.                 sw.WriteLine(str);  
  17.                 sw.Flush();  
  18.                 sw.Close();  
  19.                 fs.Close();  
  20.             }  
  21.         }  
  22.         static void Main(string[] args)  
  23.         {  
  24.             FileWrite wr = new FileWrite();  
  25.             wr.WriteData();  
  26.         }  
  27.     }  
  28. }  

StreamReader Class

 
The StreamReader class is inherited from the abstract class TextReader. The TextReader class represents a reader, which can read series of characters.
 
The following table describes some methods of the StreamReader class.
 
Methods
Description
Close
Closes the object of StreamReader class and the underlying stream, and release any system resources associated with the reader
Peek
Returns the next available character but doesn't consume it
Read
Reads the next character or the next set of characters from the stream
ReadLine
Reads a line of characters from the current stream and returns data as a string
Seek
Allows the read/write position to be moved to any position with the file
 
Program to read from a file using StreamReader Class
  1. using System;  
  2. using System.IO;  
  3. namespace FileReading_SR  
  4. {  
  5.     class Program  
  6.     {  
  7.         class FileRead  
  8.         {  
  9.             public void ReadData()  
  10.             {  
  11.                 FileStream fs = new FileStream("c:\\test.txt", FileMode.Open, FileAccess.Read);  
  12.                 StreamReader sr = new StreamReader(fs);  
  13.                 Console.WriteLine("Program to show content of test file");  
  14.                 sr.BaseStream.Seek(0, SeekOrigin.Begin);  
  15.                 string str = sr.ReadLine();  
  16.                 while (str != null)  
  17.                 {  
  18.                     Console.WriteLine(str);  
  19.                     str = sr.ReadLine();  
  20.                 }  
  21.                 Console.ReadLine();  
  22.                 sr.Close();  
  23.                 fs.Close();  
  24.             }  
  25.         }  
  26.         static void Main(string[] args)  
  27.         {  
  28.             FileRead wr = new FileRead();  
  29.             wr.ReadData();  
  30.         }  
  31.     }  
  32. }  
I hope that this article would have helped you in understanding file handling.
 


Similar Articles