Difference between FileStream and MemoryStream in .NET Core?

What is difference between FileStream & MemoryStream, key feature & code example. Write data to text file by memorystream & filestream in C#.net core.
In today's article I will explain you detail about difference between FileStream and MemoryStream in c#.net core. So, in this article we will compare about FileStream and MemoryStream about its key feature, when to use FileStream and when to MemoryStretam, compare with between different features.

What is FileStream in C#.Net Core?

  • FileStream in C#.Net is used to read and write the file. 
  • FileStream is uses disk. 
  • FileStream is class defined in namespace System.IO to read and write file as stream of byte.
  • FileStream Support synchronous and asynchronous operations.

Key Features of FileStream

Code Example:

using System.Text;
try
{
    string filepath = "C:\\Personal\\TestFile.txt";
    if (File.Exists(filepath))
    {
        using (FileStream fileStream = new FileStream(filepath, FileMode.Create))
        {
            byte[] data = Encoding.UTF8.GetBytes("Write completed successfully.");
            fileStream.Write(data, 0, data.Length);
        }
        Console.WriteLine("Write completed successfully.");
    }
    else
    {
        Console.WriteLine("Invalid file path.");
    }
}
catch (Exception ex)
{
    Console.WriteLine($"Error:{ex.Message}");
} 
In above code I have created a file and in this file, we will write some text on by using FileStream. Before writing i have validated file exists or not. Now run the code to check the output.

Success

Now let's check the text file.

Text file

What is MemoryStream in C#.Net Core?

  • MemoryStream in C#.Net is used to read the file or data in Memory (RAM).
  • As it used RAM processing of data is very fast.
  • Name space for MemoryStream is System.IO
  • It does not create a temporary file

Key Features of MemoryStream

MemoryStream Feature

Code Example

 using System.Text;
try
{
    string filepath = "C:\\Personal\\TestFile.txt";
    if (File.Exists(filepath))
    {
        byte[] data = Encoding.UTF8.GetBytes("MemoryStream Example: Write completed successfully.");
        using (MemoryStream memoryStream = new MemoryStream(data))
        {
            memoryStream.Write(data, 0, data.Length);
            memoryStream.Flush();
            memoryStream.Seek(0, SeekOrigin.Begin);

            //Write memory stream to file
            using (FileStream fileStream = new FileStream(filepath, FileMode.Create))
            {
                memoryStream.CopyTo(fileStream);
                fileStream.Flush();
            }
        }
        Console.WriteLine("Write completed successfully.");
    }
    else
    {
        Console.WriteLine("Invalid file path.");
    }
}
catch (Exception ex)
{
    Console.WriteLine($"Error:{ex.Message}");
} 
In above code i have memeorysteram to process the data in memory and after that I have used filestream to write the memorysteram value to a text file. Now lets run and check the output.

MemorySteram

Now lets check the text file for final output.

MemoryStream Example

Post a Comment