---
title: "How can you read and write to a file simultaneously using FileStream?"  
description: "How can you read and write to a file simultaneously using FileStream?"  
author: "ICSM Computer"  
published: 2025-05-05  
updated: 2025-05-05  
canonical: https://www.mindstick.com/interview/34084/how-can-you-read-and-write-to-a-file-simultaneously-using-filestream  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 2 minutes  

---

# How can you read and write to a file simultaneously using FileStream?

To **read and write to a file simultaneously** using `FileStream`, you can open the file with:

- `FileAccess.ReadWrite` (allows both reading and writing)
- A suitable `FileMode` like `Open` or `OpenOrCreate`

### Example: Read and write to the same file

```cs
using System;
using System.IO;
using System.Text;

class Program
{
    static void Main()
    {
        string path = "sample.txt";

        using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
        {
            // Write to the file
            byte[] writeData = Encoding.UTF8.GetBytes("Hello, World!");
            fs.Write(writeData, 0, writeData.Length);

            // Move the position back to the beginning before reading
            fs.Seek(0, SeekOrigin.Begin);

            // Read from the file
            byte[] readData = new byte[writeData.Length];
            fs.Read(readData, 0, readData.Length);

            Console.WriteLine("Read content: " + Encoding.UTF8.GetString(readData));
        }
    }
}
```

### Notes:

Use `fs.Seek(0, SeekOrigin.Begin)` to reposition the stream before reading.

This works for simple scenarios. For concurrent read/write from different threads, you'd need more advanced synchronization.

## Answers

### Answer by ICSM Computer

To **read and write to a file simultaneously** using `FileStream`, you can open the file with:

- `FileAccess.ReadWrite` (allows both reading and writing)
- A suitable `FileMode` like `Open` or `OpenOrCreate`

### Example: Read and write to the same file

```cs
using System;
using System.IO;
using System.Text;

class Program
{
    static void Main()
    {
        string path = "sample.txt";

        using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
        {
            // Write to the file
            byte[] writeData = Encoding.UTF8.GetBytes("Hello, World!");
            fs.Write(writeData, 0, writeData.Length);

            // Move the position back to the beginning before reading
            fs.Seek(0, SeekOrigin.Begin);

            // Read from the file
            byte[] readData = new byte[writeData.Length];
            fs.Read(readData, 0, readData.Length);

            Console.WriteLine("Read content: " + Encoding.UTF8.GetString(readData));
        }
    }
}
```

### Notes:

Use `fs.Seek(0, SeekOrigin.Begin)` to reposition the stream before reading.

This works for simple scenarios. For concurrent read/write from different threads, you'd need more advanced synchronization.


---

Original Source: https://www.mindstick.com/interview/34084/how-can-you-read-and-write-to-a-file-simultaneously-using-filestream

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
