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

WBOY
Release: 2023-08-29 16:21:04
forward
1073 people have browsed it

使用 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 };
Copy after login

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);
}
Copy after login

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);
}
Copy after login

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);
Copy after login

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);
      }
   }
}
Copy after login

Output

Are the byte arrays equal? True
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!