How to use C# FileStream class?

The FileStream class in C# provides a stream for file operations such as reading from and writing to files. It is part of the System.IO namespace and allows byte-level access to files on the file system.

FileStream is useful when you need direct control over file operations, especially for binary data or when working with large files efficiently.

Syntax

Following is the basic syntax for creating a FileStream object −

FileStream fileStream = new FileStream(path, FileMode, FileAccess);

The FileMode parameter specifies how the file should be opened −

FileMode.OpenOrCreate  // Opens existing file or creates new
FileMode.Create        // Creates new file (overwrites existing)
FileMode.Open          // Opens existing file only
FileMode.Append        // Opens for appending data

Parameters

  • path − The file path as a string

  • FileMode − Specifies how to open the file (Create, Open, OpenOrCreate, etc.)

  • FileAccess − Specifies access level (Read, Write, ReadWrite)

Using FileStream for Writing Data

Example

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

public class Demo {
    public static void Main(string[] args) {
        FileStream fstream = new FileStream("sample.txt", FileMode.OpenOrCreate);

        // Convert string to byte array and write
        string data = "Hello FileStream!";
        byte[] byteArray = Encoding.UTF8.GetBytes(data);
        fstream.Write(byteArray, 0, byteArray.Length);

        // Close the file
        fstream.Close();
        Console.WriteLine("Data written to file successfully.");
    }
}

The output of the above code is −

Data written to file successfully.

Using FileStream for Reading Data

Example

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

public class ReadDemo {
    public static void Main(string[] args) {
        // First write some data
        FileStream writeStream = new FileStream("data.txt", FileMode.Create);
        string content = "Learning C# FileStream";
        byte[] writeBytes = Encoding.UTF8.GetBytes(content);
        writeStream.Write(writeBytes, 0, writeBytes.Length);
        writeStream.Close();

        // Now read the data
        FileStream readStream = new FileStream("data.txt", FileMode.Open, FileAccess.Read);
        byte[] readBytes = new byte[readStream.Length];
        readStream.Read(readBytes, 0, (int)readStream.Length);
        readStream.Close();

        string readContent = Encoding.UTF8.GetString(readBytes);
        Console.WriteLine("File content: " + readContent);
    }
}

The output of the above code is −

File content: Learning C# FileStream

Using FileStream with Using Statement

The using statement ensures proper disposal of FileStream resources −

Example

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

public class SafeFileStream {
    public static void Main(string[] args) {
        string fileName = "safe_example.txt";
        string data = "Using statement ensures proper cleanup";

        // Write data using 'using' statement
        using (FileStream fs = new FileStream(fileName, FileMode.Create)) {
            byte[] bytes = Encoding.UTF8.GetBytes(data);
            fs.Write(bytes, 0, bytes.Length);
        } // FileStream automatically closed here

        // Read data using 'using' statement
        using (FileStream fs = new FileStream(fileName, FileMode.Open)) {
            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, (int)fs.Length);
            string result = Encoding.UTF8.GetString(buffer);
            Console.WriteLine("Read: " + result);
        }

        Console.WriteLine("File operations completed safely.");
    }
}

The output of the above code is −

Read: Using statement ensures proper cleanup
File operations completed safely.

Common FileStream Methods

Method Description
Read() Reads bytes from the file into a buffer
Write() Writes bytes from a buffer to the file
Seek() Sets the position within the file
Flush() Forces buffered data to be written to disk
Close() Closes the file and releases resources

Conclusion

The FileStream class provides low-level file access in C# for reading and writing byte data. Always use the using statement or explicitly call Close() to properly release file handles and avoid resource leaks. For text operations, consider higher-level classes like StreamReader and StreamWriter.

Updated on: 2026-03-17T07:04:35+05:30

407 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements