Write to a binary file in Java
This post will discuss how to write to a binary file in Java.
The solution should create the file if it doesn’t exist or truncate the file before writing if it already exists. Following are some available options in Java to write bytes to a binary file.
1. Using Files Class
With Java 7, we can use Files.write(…), which writes bytes to a file efficiently. It opens the file for writing, creating the file if it doesn’t exist, or initially truncating an existing file to size 0.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; class Main { public static void main(String[] args) { Path path = Paths.get("doc.txt"); byte[] bytes = "ABCD".getBytes(StandardCharsets.UTF_8); try { Files.write(path, bytes); // Java 7+ only System.out.println("Successfully written data to the file"); } catch (IOException e) { e.printStackTrace(); } } } |
2. Using FileOutputStream
FileOutputStream is meant for writing streams of raw bytes such as image data. Here’s a working example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; class Main { public static void main(String[] args) { File file = new File("input.txt"); byte[] data = "ABCD".getBytes(StandardCharsets.UTF_8); try (FileOutputStream fos = new FileOutputStream(file)) { fos.write(data); System.out.println("Successfully written data to the file"); } catch (IOException e) { e.printStackTrace(); } } } |
3. Using DataOutputStream
We can use a data output stream to write the string to the underlying output stream as a bytes sequence.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.io.*; class Main { public static void main(String[] args) { File file = new File("foo.out"); String text = "ABCD"; try (FileOutputStream fos = new FileOutputStream(file); BufferedOutputStream bos = new BufferedOutputStream(fos); DataOutputStream dos = new DataOutputStream(bos)) { dos.writeBytes(text); System.out.println("Successfully written data to the file"); } catch (IOException e) { e.printStackTrace(); } } } |
4. Using Guava Library
Guava’s Files.write(byte[], File) method can be used for overwriting a file with the contents of a byte array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import com.google.common.io.Files; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; class Main { public static void main(String[] args) { File file = new File("doc.txt"); byte[] data = "ABCD".getBytes(StandardCharsets.UTF_8); try { Files.write(data, file); System.out.println("Successfully written data to the file"); } catch (IOException e) { e.printStackTrace(); } } } |
5. Using Apache Commons IO
FileUtils class from Apache Commons IO library has the writeByteArrayToFile(File, byte[]) method, which writes a byte array to a file. If the file already exists, then the file will be truncated before writing.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; class Main { public static void main(String[] args) { File file = new File("foo.out"); byte[] data = "ABCD".getBytes(StandardCharsets.UTF_8); try { FileUtils.writeByteArrayToFile(file, data); System.out.println("Successfully written data to the file"); } catch (IOException e) { e.printStackTrace(); } } } |
6. Using FileChannel for large files
To write large binary files, use FileChannel. Note this doesn’t overwrite an existing file.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.StandardCharsets; class Main { public static void main(String[] args) { File file = new File("doc.txt"); byte[] data = "ABCD".getBytes(StandardCharsets.UTF_8); try (RandomAccessFile stream = new RandomAccessFile(file, "rw"); FileChannel channel = stream.getChannel()) { ByteBuffer buffer = ByteBuffer.allocate(data.length); buffer.put(data); buffer.flip(); channel.write(buffer); System.out.println("Successfully written data to the file"); } catch (IOException e) { e.printStackTrace(); } } } |
That’s all about writing to a binary file in Java.
Also See:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)