C# FileStream

In C#, the FileStream class is used to read from and write to files. It provides a low-level stream for interacting with files, allowing you to perform various operations such as reading bytes, writing bytes, and seeking within the file. Here’s an example of how to use FileStream:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = "example.txt";

        // Writing to a file using FileStream
        using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
        {
            string data = "Hello, FileStream!";
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data);
            fileStream.Write(bytes, 0, bytes.Length);
        }

        // Reading from a file using FileStream
        using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
        {
            byte[] buffer = new byte[fileStream.Length];
            fileStream.Read(buffer, 0, buffer.Length);
            string data = System.Text.Encoding.UTF8.GetString(buffer);
            Console.WriteLine(data);
        }
    }
}

In the example above, we first create a FileStream object with the file path and FileMode.Create to open the file for writing. We convert the string data into bytes using the UTF8 encoding and then write those bytes to the file using the Write method.

Next, we create another FileStream object with the same file path but using FileMode.Open to open the file for reading. We allocate a byte array to hold the contents of the file and use the Read method to read the bytes from the file into the buffer. Finally, we convert the byte array back to a string using the UTF8 encoding and display it on the console.

Remember to include the System.IO namespace to access the FileStream class and other related classes for working with files.

C# FileStream example: writing single byte into file

Certainly! Here’s an example of using FileStream in C# to write a single byte to a file:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = "example.txt";

        // Writing a single byte to a file using FileStream
        using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
        {
            byte singleByte = 65; // ASCII value for 'A'
            fileStream.WriteByte(singleByte);
        }

        Console.WriteLine("Byte written to the file.");
    }
}

In this example, we create a FileStream object with the file path and FileMode.Create to open the file for writing. We then specify the byte we want to write (in this case, the ASCII value for the letter ‘A’) and use the WriteByte method to write that single byte to the file.

Finally, we display a message on the console to indicate that the byte has been written to the file.

Remember to include the System.IO namespace to access the FileStream class and other related classes for working with files.

C# FileStream example: writing multiple bytes into file

Certainly! Here’s an example of using FileStream in C# to write multiple bytes to a file:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = "example.txt";

        // Writing multiple bytes to a file using FileStream
        using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
        {
            byte[] bytes = { 65, 66, 67, 68 }; // ASCII values for 'A', 'B', 'C', 'D'
            fileStream.Write(bytes, 0, bytes.Length);
        }

        Console.WriteLine("Bytes written to the file.");
    }
}

In this example, we create a FileStream object with the file path and FileMode.Create to open the file for writing. We define an array of bytes containing the values 65, 66, 67, and 68, which correspond to the ASCII values for the characters ‘A’, ‘B’, ‘C’, and ‘D’ respectively.

We then use the Write method of the FileStream object to write the entire byte array to the file, starting from index 0 and writing all the bytes in the array.

Finally, we display a message on the console to indicate that the bytes have been written to the file.

Remember to include the System.IO namespace to access the FileStream class and other related classes for working with files.

C# FileStream example: reading all bytes from file

Certainly! Here’s an example of using FileStream in C# to read all bytes from a file:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = "example.txt";

        // Reading all bytes from a file using FileStream
        using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
        {
            byte[] buffer = new byte[fileStream.Length];
            int bytesRead = fileStream.Read(buffer, 0, buffer.Length);

            // Convert bytes to string for display
            string data = System.Text.Encoding.UTF8.GetString(buffer);

            Console.WriteLine("Bytes read from the file: " + bytesRead);
            Console.WriteLine("Content of the file: " + data);
        }
    }
}

In this example, we create a FileStream object with the file path and FileMode.Open to open the file for reading. We allocate a byte array buffer with a size equal to the length of the file using the Length property of the FileStream object.

Next, we use the Read method of the FileStream object to read all the bytes from the file into the buffer. The method returns the number of bytes read, which we store in the bytesRead variable.

To display the content of the file, we convert the byte array to a string using the UTF8 encoding and store it in the data variable.

Finally, we print the number of bytes read from the file and the content of the file on the console.

Remember to include the System.IO namespace to access the FileStream class and other related classes for working with files.