FileStream in C#

FileStream Class Provides a Stream for a file, supporting both synchronous and asynchronous read and write operations, so FileStream class is used to perform the basic operation of reading and writing operating system files. FileStream class helps in reading from, writing and closing files.

Example of File Stream:-

using System;
using System.IO;
using System.Text;

class FileStreamTestExample
{
    public static void Main()
    {
        string path = @"c:\temp\MyFileStreamTestFile.txt";

        // Delete the file if it exists.
        if (File.Exists(path))
        {
            File.Delete(path);
        }

        //Create the file.
        using (FileStream fs = File.Create(path))
        {
            AddTextToFile(fs, "This is line one text ");
            AddTextToFile(fs, "hi this is nitin kumar   ");
            AddTextToFile(fs, "some other text ........");
        }

        //Open the stream and read it back.
        using (FileStream fs = File.OpenRead(path))
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);
            while (fs.Read(b, 0, b.Length) > 0)
            {
                Console.WriteLine(temp.GetString(b));
            }
        }
    }
    private static void AddTextToFile(FileStream fs, string value)
    {
        byte[] info = new UTF8Encoding(true).GetBytes(value);
        fs.Write(info, 0, info.Length);
    }
}