Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until tomorrow:


November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until tomorrow:


1. Overview

In this quick tutorial, we're going to learn several different ways to write a Java byte array to a file. We'll start at the beginning, using the Java IO package. Next, we'll look at an example using Java NIO. After that, we'll use Google Guava and Apache Commons IO.

2. Java IO

Java's IO package has been around since JDK 1.0 and provides a collection of classes and interfaces for reading and writing data.

Let's use a FileOutputStream to write the image to a file:

File outputFile = tempFolder.newFile("outputFile.jpg");
try (FileOutputStream outputStream = new FileOutputStream(outputFile)) {

We open an output stream to our destination file, and then we can simply pass our byte[] dataForWriting to the write method. Note that we're using a try-with-resources block here to ensure that we close the OutputStream in case an IOException is thrown.

3. Java NIO

The Java NIO package was introduced in Java 1.4, and the file system API for NIO was introduced as an extension in Java 7. Java NIO is uses buffering and is non-blocking, whereas Java IO uses blocking streams. The syntax for creating file resources is more succinct in the java.nio.file package.

We can write our byte[] in one line using the Files class:

Files.write(outputFile.toPath(), dataForWriting);

Our example either creates a file or truncates an existing file and opens it for write. We can also use Paths.get(“path/to/file”) or Paths.get(“path”, “to”, “file”) to construct the Path that describes where our file will be stored. Path is the Java NIO native way of expressing paths.

If we need to override the file opening behavior, we can also provide OpenOption to the write method.

4. Google Guava

Guava is a library by Google that provides a variety of types for performing common operations in Java, including IO.

Let's import Guava into our pom.xml file:


4.1. Guava Files

As with the Java NIO package, we can write our byte[] in one line:

Files.write(dataForWriting, outputFile);

Guava's Files.write method also takes an optional OptionOptions and uses the same defaults as java.nio.Files.write.

There's a catch here though: The Guava Files.write method is marked with the @Beta annotation. According to the documentation, that means it can change at any time and so is not recommended for use in libraries.

So, if we're writing a library project, we should consider using a ByteSink.

4.2. ByteSink

We can also create a ByteSink to write our byte[]:

ByteSink byteSink = Files.asByteSink(outputFile);

The ByteSink is a destination to which we can write bytes. It supplies an OutputStream to the destination.

If we need to use a java.nio.files.Path or to supply a special OpenOption, we can get our ByteSink using the MoreFiles class:

ByteSink byteSink = MoreFiles.asByteSink(outputFile.toPath(), 

5. Apache Commons IO

Apache Commons IO provides some common file tasks.

Let's import the latest version of commons-io:


Now, let's write our byte[] using the FileUtils class:

FileUtils.writeByteArrayToFile(outputFile, dataForWriting);

The FileUtils.writeByteArrayToFile method is similar to the other methods that we've used in that we give it a File representing our desired destination and the binary data we're writing. If our destination file or any of the parent directories don't exist, they'll be created.

6. Conclusion

In this short tutorial, we learned how to write binary data from a byte[] to a file using plain Java and two popular Java utility libraries: Google Guava and Apache Commons IO.

As always, the example code is available over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until tomorrow:


Generic footer banner
Comments are closed on this article!