CKTIS / Java
Binary data streams: reading and writing bytes, numbers, and strings
Read fields in the same order you wrote them. See how Java SE 21 lays out numeric values and why the `writeUTF` limit counts encoded bytes, not characters.
Version: Java SE 21
Syntax
Java SE 21 · java.io
DataOutputStream(OutputStream out)
void writeByte(int v) throws IOException
void writeInt(int v) throws IOException
void writeDouble(double v) throws IOException
void writeUTF(String str) throws IOException
DataInputStream(InputStream in)
byte readByte() throws IOException
int readInt() throws IOException
double readDouble() throws IOException
String readUTF() throws IOException
static String readUTF(DataInput in) throws IOExceptionIn Java SE 21, DataOutputStream and DataInputStream in java.io wrap an output and an input stream, respectively. The DataOutputStream.writeByte/writeInt/writeDouble/writeUTF methods write the fields, and DataInputStream.readByte/readInt/readDouble/readUTF reads them back in the same order and with matching types. The stream carries no type or version tags: reading a different type or order can return wrong values without throwing.
writeByte(int) writes only the low eight bits, with no range check. readByte() returns a signed byte from −128 to 127; use readUnsignedByte() instead to get an int from 0 to 255. writeInt(int) and readInt() use four bytes for an int in big-endian order, most significant byte first. writeDouble(double) and readDouble() use eight IEEE 754 binary64 bytes in the same order. Writing uses Double.doubleToLongBits, which canonicalizes NaNs; reading uses Double.longBitsToDouble.
writeUTF(String) writes an unsigned two-byte big-endian length followed by modified UTF-8. The limit is 65,535 payload bytes, excluding the prefix, not characters or String.length(). This is not standard UTF-8: U+0000 takes two bytes, and a supplementary character takes six, three for each UTF-16 code unit. The instance method readUTF() reads from the wrapped stream; the static overload DataInputStream.readUTF(DataInput) reads the same format from the supplied source.
On writing, an oversized encoded string causes UTFDataFormatException, and null causes NullPointerException. On reading, premature end within a field, length prefix, or string payload causes EOFException; detected malformed encoding causes UTFDataFormatException. Both are subtypes of IOException. Closing a wrapper closes its underlying stream; use try-with-resources when you own the stream.
The layout does not depend on CPU byte order or the platform's default charset, but a non-Java reader still has to implement the same format. Validate field values from untrusted records against your application's rules: successful decoding does not make the values acceptable.
Examples
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.HexFormat;
public class DataStreamDemo {
public static void main(String[] args) throws IOException {
String label = "A\u00E9";
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
try (var out = new DataOutputStream(buffer)) {
out.writeByte(255);
out.writeInt(0x01020304);
out.writeDouble(1.5);
out.writeUTF(label);
}
byte[] record = buffer.toByteArray();
System.out.println(HexFormat.ofDelimiter(" ").formatHex(record));
try (var in = new DataInputStream(new ByteArrayInputStream(record))) {
System.out.println(in.readByte());
System.out.println(in.readInt());
System.out.println(in.readDouble());
System.out.println(in.readUTF().equals(label));
}
}
}Result
ff 01 02 03 04 3f f8 00 00 00 00 00 00 00 03 41 c3 a9 -1 16909060 1.5 true
The program prints the record bytes, then the decoded values and the string equality check. The byte `ff` reads back as signed `-1`, and `00 03` announces three payload bytes even though the string has two UTF-16 code units.
Related lessons
- Java · Lesson 53 · Advanced · Version: 2