Read and Write Binary Data in Java
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.
Java Program
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
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.
- 1
writeIntwrites 4 bytes,writeDouble8 andwriteBoolean1. - 2
writeUTFwrites a 2-byte length followed by the text's bytes. - 3The file size, 23 bytes, is the sum of those parts.
- 4
readInt,readDouble,readBooleanandreadUTFread them back in the same order.
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
Approach 2: Raw Byte Arrays
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
Core Logic
For raw data such as image pixels, Files.write and Files.readAllBytes move a whole byte array in one call.
- 1A small array of pixel values is written as raw bytes.
- 2
readAllBytesloads it back. - 3
b & 0xFFconverts Java's signed bytes (−128 to 127) into 0–255 values.
& 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
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.