Java Programs›File Handling›Read and Write Binary Data

Read and Write Binary Data in Java

intermediate·  File Handling  ·  File I/O

Problem

Binary files store values in their raw byte form — an int is always 4 bytes — instead of as human-readable text.

Save a sensor reading made of different value types to a binary file, then read each value back.

Input
42, 36.6, true, "sensor-A"
Output
23-byte file, values read back intact

Java Program

Java
import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class ReadWriteBinaryData { public static void main(String[] args) throws IOException { Path file = Paths.get("reading.bin"); try (DataOutputStream out = new DataOutputStream(Files.newOutputStream(file))) { out.writeInt(42); // 4 bytes out.writeDouble(36.6); // 8 bytes out.writeBoolean(true); // 1 byte out.writeUTF("sensor-A"); // 2-byte length + the characters } System.out.println("File size: " + Files.size(file) + " bytes"); try (DataInputStream in = new DataInputStream(Files.newInputStream(file))) { // must read back in exactly the same order and types System.out.println("int: " + in.readInt()); System.out.println("double: " + in.readDouble()); System.out.println("boolean: " + in.readBoolean()); System.out.println("text: " + in.readUTF()); } Files.delete(file); } }

Output

File size: 23 bytes int: 42 double: 36.6 boolean: true text: sensor-A

Core Logic

Data streams write each Java type in a fixed binary layout, so reading in the same order and types restores the exact values.

How It Works
  1. 1writeInt writes 4 bytes, writeDouble 8 and writeBoolean 1.
  2. 2writeUTF writes a 2-byte length followed by the text's bytes.
  3. 3The file size, 23 bytes, is the sum of those parts.
  4. 4readInt, readDouble, readBoolean and readUTF read them back in the same order.
4 + 8 + 1 + (2 + 8) = 23 bytes.
💡

Key Point: Reading values in a different order or type doesn't throw an error — it silently produces garbage, so writer and reader must agree on the layout.

Key Concepts

DataOutputStreamDataInputStreamwriteUTF()fixed-size types

Approach 2: Raw Byte Arrays

Java
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; public class ReadWriteBinaryBytes { public static void main(String[] args) throws IOException { Path file = Paths.get("pixels.bin"); byte[] pixels = {0, 64, (byte) 128, (byte) 255, 32}; Files.write(file, pixels); // raw bytes, no text encoding involved byte[] back = Files.readAllBytes(file); int[] unsigned = new int[back.length]; for (int i = 0; i < back.length; i++) unsigned[i] = back[i] & 0xFF; // bytes are signed in Java System.out.println("Raw bytes: " + Arrays.toString(back)); System.out.println("As 0-255 values: " + Arrays.toString(unsigned)); Files.delete(file); } }

Output

Raw bytes: [0, 64, -128, -1, 32] As 0-255 values: [0, 64, 128, 255, 32]

Core Logic

For raw data such as image pixels, Files.write and Files.readAllBytes move a whole byte array in one call.

How It Works
  1. 1A small array of pixel values is written as raw bytes.
  2. 2readAllBytes loads it back.
  3. 3b &amp; 0xFF converts Java's signed bytes (−128 to 127) into 0–255 values.
The byte 128 is stored as −128 in Java, and &amp; 0xFF turns it back into 128.
💡

Key Point: Java's byte is signed, so always mask with 0xFF when you need unsigned values.

Key Concepts

Files.write(byte[])Files.readAllBytes()signed bytes

Interview Questions

Why use binary files instead of text?

They're smaller, faster to read and keep exact values — a double never loses precision through text formatting.

What byte order do DataOutputStream values use?

Big-endian (most significant byte first). Use ByteBuffer with ByteOrder.LITTLE_ENDIAN to match formats that need the other order.

Learn the Concept

Related Programs