HOWTO · Java

How to Write Byte to File in Java

This article shows how to write byte to file in Java

On this page

This tutorial demonstrates how to write a byte array to a file in Java. The standard library offers two practical options — a FileOutputStream for stream-style writes and the NIO Files.write() method for a single-call operation — and Apache Commons IO adds FileUtils.writeByteArrayToFile() as a convenience for projects that already carry the dependency. Every example converts text to bytes with StandardCharsets.UTF_8 so the stored file does not depend on the machine’s default charset.

Write a Byte Array with FileOutputStream

FileOutputStream is the classic byte-oriented stream in java.io. Constructing it on a file name creates the file when it is missing and replaces the contents of a file that is already there; pass true as a second constructor argument when you want to append instead. The stream holds an operating-system handle, so close it when the write finishes — try-with-resources performs the close automatically.

Run this example:

import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;

class ByteToFile {
  public static void main(String[] args) throws Throwable {
    byte[] data = "DelftStack writes bytes".getBytes(StandardCharsets.UTF_8);
    try (FileOutputStream out = new FileOutputStream("stream-demo.bin")) {
      out.write(data);
    }
    System.out.println("Wrote " + data.length + " bytes to stream-demo.bin");
  }
}
Wrote 23 bytes to stream-demo.bin

getBytes(StandardCharsets.UTF_8) encodes the string deterministically; calling getBytes() with no argument uses the platform default charset and could produce different bytes on another machine. write(data) stores the whole array, while write(data, offset, length) stores only a slice when you need one. When the constructor itself fails — a path that names a directory, a parent directory that does not exist, or missing write permission — Java raises a FileNotFoundException, so pick a writable destination before opening the stream.

Write a Byte Array with Files.write()

The java.nio.file API compresses the whole operation into one call. Files.write() opens the path, writes every byte, and closes the file; by default it creates the file when absent and truncates it when present. Pass open options such as StandardOpenOption.APPEND to append or StandardOpenOption.CREATE_NEW to fail when the file already exists.

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

class ByteToFileNio {
  public static void main(String[] args) throws Throwable {
    byte[] data = "NIO write sample data".getBytes(StandardCharsets.UTF_8);
    Path target = Paths.get("nio-demo.bin");
    Files.write(target, data);
    System.out.println("Wrote " + data.length + " bytes to nio-demo.bin");
  }
}
Wrote 21 bytes to nio-demo.bin

The method declares IOException, and its common failures are descriptive: NoSuchFileException when a parent directory is missing, AccessDeniedException when the process lacks permission, and FileAlreadyExistsException when you pass CREATE_NEW against an existing file. Choose Files.write() when you want the shortest standard-library form and do not need the open-stream control of FileOutputStream.

Write a Byte Array with Apache Commons IO

Projects that already depend on Apache Commons IO can call FileUtils.writeByteArrayToFile() instead. Declare the Maven coordinates commons-io:commons-io with the version your build pins — the original version of this article showed 2.6, which is a supplied example version rather than a current recommendation, so check the release line you intend to use.

With org.apache.commons.io.FileUtils imported, the call is FileUtils.writeByteArrayToFile(new File("doc.txt"), data), where data is any byte[] such as "text".getBytes(StandardCharsets.UTF_8). The method creates the destination file when it is missing, creates missing parent directories along the way — a step Files.write() does not perform — and truncates an existing file before writing. It also manages the stream internally, so no handle is left for the caller to close. Because the class lives in a third-party jar, this snippet is shown as source rather than as a runnable example; for new code without the dependency, Files.write() covers the same task in one line.