Showing posts with label define c# I/O. Show all posts
Showing posts with label define c# I/O. Show all posts

Wednesday, August 10, 2016

File I/O in C#



File is the collection of data that is stored in a disk with specific name, when we open a file for read or write the data it becomes a steam.

The steam is the sequence of the byte passing through the communication path, there are two types of steam input steam and output steam.

Input steam – Input steam is used to read the data from the file.
Output steam – Output steam is used to write the data into file.

C# I/O Classes – In C# system.IO name space is used to performing operation with file.System.IO name space contains following classes.

BinaryReader – Used to read the primitive data from binary steam
Binarywriter – Used to write the primitive data in binary format.
Directory – Used to manipulating a directory structurer.
DirectoryInfo – Used to perform operation on directories.
DriveInfo – It gives the information for the drives.
File – Used for manipulating files.
FileInfo – It is used to perform operation on file.
Filesteam – Used to read and write from the file.
Path – Perform operation on path information.
SteamReader – It is used to read character from a byte steam.
Steamwriter – It is used to writing character to a steam.
StringReader – Used to read from string buffer.
StringWriter – Used to write into a string buffer.

Example – the following program is used for Filesteam class - 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication19
{
    class Program
    {
        FileStream FS = new FileStream("test.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite);

         for (int i = 1; i<= 20; i++)
         {
            FS.WriteByte((byte)i);
         }
        
         FS.Position = 0;
         for (int i = 0; i <= 20; i++)
         {
            Console.Write(F.ReadByte() + " ");
         }
         FS.Close();
         Console.ReadKey();
      }
}
Output - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -1

                                                             Author - Sachin Pathak