Java Reference ยท I/O and Files
DataInputStream and DataOutputStream
Reference for DataInputStream and DataOutputStream: write and read primitives and strings in a portable binary format, with writeUTF, readFully and EOF handling.
DataOutputStream writes Java primitives (int, long, double, boolean, and so on) and strings to an underlying OutputStream in a fixed, platform-independent binary format. DataInputStream reads them back. You must read values in the same order and with the same types as you wrote them, because the stream contains no labels.
The format is big-endian: an int takes four bytes with the most significant byte first. That is the same as network byte order, which makes these classes handy for simple binary protocols and small file formats.
DataOutputStream out = new DataOutputStream(stream); out.writeInt(7); out.writeUTF("hello");
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public class DataOutputStream extends FilterOutputStream implements DataOutput public class DataInputStream extends FilterInputStream implements DataInput DataOutputStream(OutputStream out) DataInputStream(InputStream in)
| Write method | Bytes | Matching read method |
|---|---|---|
writeByte(int) |
1 | readByte(), readUnsignedByte() |
writeShort(int) |
2 | readShort(), readUnsignedShort() |
writeChar(int) |
2 | readChar() |
writeInt(int) |
4 | readInt() |
writeLong(long) |
8 | readLong() |
writeFloat(float) |
4 | readFloat() |
writeDouble(double) |
8 | readDouble() |
writeBoolean(boolean) |
1 | readBoolean() |
writeUTF(String) |
2 + length | readUTF() |
write(byte[]) |
n | readFully(byte[]) |
size() on the output stream reports the bytes written so far. All methods throw IOException; the read methods throw EOFException when the stream ends before the value is complete.
Usage notes
- Order is the contract. There are no field names or types in the data; reading a
longwhere anintwas written gives garbage, not an error. - Big-endian always. To read or write little-endian data (many file formats, x86 native layouts) use
ByteBuffer.order(ByteOrder.LITTLE_ENDIAN)from NIO buffers. writeUTFuses "modified UTF-8" with a two-byte length prefix, so a string can hold at most 65,535 encoded bytes (UTFDataFormatExceptionotherwise), and\u0000and supplementary characters are encoded differently from standard UTF-8. It is meant for this class's own format, not for interchange. For a standard encoding write bytes:s.getBytes(UTF_8)preceded by its length.writeBytes(String)andwriteChars(String)exist but are rarely what you want: the first drops the high byte of each char.- Detecting the end:
read()returns-1, but thereadXmethods throwEOFException. Write a record count first, or catchEOFExceptionas the loop end. readFullyversusread:read(byte[])may return fewer bytes;readFullyblocks until the array is full.- Buffering: wrap the underlying stream in
BufferedOutputStreamorBufferedInputStream, because everywriteIntis otherwise several small writes. - Not versioned: if the format may change, start the data with a version number.