Home  >  Article  >  Backend Development  >  C# program to read a byte array and write it to a file using the FileStream class

C# program to read a byte array and write it to a file using the FileStream class

WBOY
WBOYforward
2023-08-29 16:21:041059browse

使用 FileStream 类读取字节数组并将其写入文件的 C# 程序

C# is a powerful object-oriented programming language used for developing various applications. In this article, we will discuss how to write a C# program to read and write a byte array to a file using the FileStream class.

Step one: Create a byte array

The first step in the program is to create a byte array that we want to write to the file. Here is an example -

byte[] byteArray = { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64 };

Step 2: Write byte array to file

The next step is to use the FileStream class to write the byte array to the file. We need to create a new instance of the FileStream class and pass the file path, FileMode, FileAccess and FileShare as parameters to its constructor. Here is an example -

string filePath = "C:\MyFile.txt";
using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None)) {
   fileStream.Write(byteArray, 0, byteArray.Length);
}

Step 3: Read byte array from file

To read a byte array from a file, we need to create a new instance of the FileStream class and pass the file path, FileMode, FileAccess, and FileShare as parameters to its constructor. We then create a byte array and read the contents of the file into the byte array using the Read() method of the FileStream class. Here is an example -

byte[] readByteArray = new byte[byteArray.Length];
using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) {
   fileStream.Read(readByteArray, 0, readByteArray.Length);
}

Step 4: Compare byte arrays

Finally, we need to compare the original byte array and the byte array read from the file to make sure they are the same. We can compare two byte arrays using the SequenceEqual() method of the Enumerable class. Here is an example -

bool areEqual = byteArray.SequenceEqual(readByteArray);

Example

This is the complete C# program -

using System;
using System.IO;
using System.Linq;

namespace ByteArrayToFile {
   class Program {
      static void Main(string[] args) {
         byte[] byteArray = { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64 };
         string filePath = "C:\MyFile.txt";
         
         // Write byte array to file
         using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None)) {
            fileStream.Write(byteArray, 0, byteArray.Length);
         }
         
         // Read byte array from file
         byte[] readByteArray = new byte[byteArray.Length];
         using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) {
            fileStream.Read(readByteArray, 0, readByteArray.Length);
         }

         // Compare the byte arrays
         bool areEqual = byteArray.SequenceEqual(readByteArray);
         Console.WriteLine("Are the byte arrays equal? " + areEqual);
      }
   }
}

Output

Are the byte arrays equal? True

in conclusion

In this article, we learned how to write a C# program using the FileStream class to read and write byte arrays to files. This program can be used in a variety of scenarios, such as reading and writing image or audio files. By understanding the concepts covered in this article, you can develop more advanced applications that require file input and output. I hope this article has been helpful in your programming journey. Happy coding!

The above is the detailed content of C# program to read a byte array and write it to a file using the FileStream class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Object initializer in C#Next article:Object initializer in C#