- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Program to Read and Write a Byte Array to File using FileStream Class
C# is a powerful object-oriented programming language used for developing a wide range of 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 1: Creating a Byte Array
The first step in this program is to create a byte array that we want to write to a file. Here is an example −
byte[] byteArray = { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64 };
Step 2: Writing Byte Array to File
The next step is to write the byte array to a file using the FileStream class. We need to create a new instance of the FileStream class and pass the file path, FileMode, FileAccess, and FileShare as arguments 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: Reading Byte Array From File
To read the byte array from the file, we need to create a new instance of the FileStream class and pass the file path, FileMode, FileAccess, and FileShare as arguments 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: Comparing the Byte Arrays
Finally, we need to compare the original byte array and the byte array read from the file to make sure that they are the same. We can use the SequenceEqual() method of the Enumerable class to compare the two byte arrays. Here is an example −
bool areEqual = byteArray.SequenceEqual(readByteArray);
Example
Here 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
Conclusion
In this article, we learned how to write a C# program to read and write a byte array to a file using the FileStream class. This program can be used in a variety of scenarios, such as reading and writing images 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 to you in your programming journey. Happy coding!