Oftentimes, we find ourselves with a chunk of data in a byte array that needs to be stored in a file. This is usually for the sake of storage or for further processing. Let’s consider a few different ways to convert a byte array to a file.

To download the source code for this article, you can visit our GitHub repository.

Let’s dive in.

Using BinaryWriter and FileStream

The BinaryWriter is simply a class that helps to store strings of different encodings as well as raw binary data into files or memory locations.

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!
Become a patron at Patreon!

Let’s write a method to save a byte array using the BinaryWriter class:

public static void SaveByteArrayToFileWithBinaryWriter(byte[] data, string filePath)
{
    using var writer = new BinaryWriter(File.OpenWrite(filePath));
    writer.Write(data);
}

We pass the FileStream class into the BinaryWriter object and then call on the BinaryWriter to help us write the data into the stream.

Using FileStream Only

The FileStream can manipulate data in a class itself. It has methods that can help us write data into a file but has a limited number of options in comparison to the BinaryWriter class.

Since we are simply working with byte arrays here, we can go ahead and implement our next method using FileStream alone:

public static void SaveByteArrayToFileWithFileStream(byte[] data, string filePath)
{
    using var stream = File.Create(filePath);
    stream.Write(data, 0, data.Length);
}

Our method simply creates a FileStream object passing in the intended file path as the only argument. Then, it uses the instance method called Write() to write the byte array into the created file.

It is imperative that we take note of the way we’re utilizing using var to declare the variables in the first two approaches above. This is because it helps us release the locked system resources once we are done using them.

Let’s explore the last option on our list.

Using File.WriteAllBytes() Method

The static File class in the System.IO namespace provides a simple static method WriteAllBytes() that we can use to write all the data from a byte array to a file.

Let’s write a new method that makes use of this:

public static void SaveByteArrayToFileWithStaticMethod(byte[] data, string filePath)
  => File.WriteAllBytes(filePath, data);

The entire implementation is within the File class, hence, making it shorthand and straightforward.

Conclusion 

We have explored various ways to convert a byte array to a file. Apparently, there is no clear-cut way to determine the best choice.

Nevertheless, you should always choose based on your needs. The last approach is great when you just need to save a file quickly and move on to something else. However, if you need to perform some further action or processing on the stream, you should consider either of the first two.

Liked it? Take a second to support Code Maze on Patreon and get the ad free reading experience!
Become a patron at Patreon!