C# FileStream

In c#, FileStream is a class of System.IO namespace, and it will provide a stream for file operations. By using FileStream, we can perform read and write operations on files.

 

Now, we will see how to use FileStream class in c# to write and read text from a file with examples.

C# FileStream Write to File Example

Following is the example of writing a text to file using the FileStream object in c#.

 

using System.Text;
using System.IO;

namespace TutlaneExamples
{
    class Program
    {
       static void Main(string[] args)
       {
          string fpath = @"D:\Test.txt";
          // Delete file if exists
          if (File.Exists(fpath))
          {
              File.Delete(fpath);
          }
          // Create the file
          using (FileStream fs = File.Create(fpath))
          {
              AddTexttoFile(fs, "Hi");
              AddTexttoFile(fs, "\r\nWelcome to Tutlane");
              AddTexttoFile(fs, "\r\nFileStream Example");
          }
       }
       private static void AddTexttoFile(FileStream fs, string value)
       {
          byte[] info = new UTF8Encoding(true).GetBytes(value);
          fs.Write(info, 0, info.Length);
       }
    }
}

If you observe the above example, we imported a System.IO namespace to access File & FileStream objects to delete, create and write a text to file. Here, we added text to the file by converting it into bytes format using the AddTexttoFile method.

 

When we execute the above example, it will create a new “Test.txt” file in the D drive with the required text as shown below.

 

C# FileStream Write to File Example Result

 

This is how we can create, delete and add text to files using File & FileStream objects in c#. 

C# FileStream Read Text from File Example

Following is the example of reading text from a file using the FileStream object in c#.

 

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

namespace TutlaneExamples
{
    class Program
    {
       static void Main(string[] args)
       {
          string fpath = @"D:\Test.txt";
          // Check if file exists
          if (File.Exists(fpath))
          {
             // Open the file and read
             using (FileStream fs = File.OpenRead(fpath))
             {
                byte[] b = new byte[1024];
                UTF8Encoding encode = new UTF8Encoding(true);
                while (fs.Read(b, 0, b.Length) > 0)
                {
                   Console.WriteLine(encode.GetString(b));
                }
             }
          }
          Console.ReadLine();
       }
    }
}

If you observe the above example, we imported a System.IO namespace to access File & FileStream objects to check and read text from a file.

 

When you execute the above example, it will read the text from “Test.txt” file in D drive and return the result as shown below.

 

C# FileStream Read Text from File Example Result

 

This is how we can use FileStream class in c# to write or read data from the file in the file system.