Java : DataOutputStream with Examples

DataOutputStream (Java SE 21 & JDK 21) with Examples.
You will find code examples on most DataOutputStream methods.


Summary

A data output stream lets an application write primitive Java data types to an output stream in a portable way. An application can then use a data input stream to read the data back in.

Class diagram

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeByte(0xff);
    os.writeInt(0x12345678);
    os.writeUTF("abcd");
}

final var bytes = out.toByteArray();

// [ff, 12, 34, 56, 78, 00, 04, 61, 62, 63, 64]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    final var b = is.readByte();
    System.out.printf("%x%n", b); // ff

    final var i = is.readInt();
    System.out.printf("%x%n", i); // 12345678

    final var utf = is.readUTF();
    System.out.println(utf); // abcd
}

Fields

protected int written

The number of bytes written to the data output stream so far.

class MyOutputStream extends DataOutputStream {
    MyOutputStream(OutputStream out) {
        super(out);
    }

    int getWritten() {
        return written;
    }
}

final var out = new ByteArrayOutputStream();
try (final var os = new MyOutputStream(out)) {
    os.writeByte(0x10);
    System.out.println(os.getWritten()); // 1

    os.writeByte(0x20);
    System.out.println(os.getWritten()); // 2

    os.writeInt(0x12345678);
    System.out.println(os.getWritten()); // 6
}

final var bytes = out.toByteArray();

// [10, 20, 12, 34, 56, 78]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

Fields declared in FilterOutputStream

out

Please see the link below.

Constructors

DataOutputStream (OutputStream out)

Creates a new data output stream to write data to the specified underlying output stream.

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeByte(0x10);
    os.writeByte(0x20);
    os.writeInt(0x12345678);
}

final var bytes = out.toByteArray();

// [10, 20, 12, 34, 56, 78]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

Methods

void flush ()

Flushes this data output stream.

final var out = new ByteArrayOutputStream();

try (final var os = new DataOutputStream(new BufferedOutputStream(out))) {

    os.write(10);
    System.out.println(Arrays.toString(out.toByteArray())); // []

    os.flush();
    System.out.println(Arrays.toString(out.toByteArray())); // [10]

    os.write(20);
    System.out.println(Arrays.toString(out.toByteArray())); // [10]

    os.flush();
    System.out.println(Arrays.toString(out.toByteArray())); // [10, 20]

    os.write(30);
    System.out.println(Arrays.toString(out.toByteArray())); // [10, 20]
}

System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30]

final int size ()

Returns the current value of the counter written, the number of bytes written to this data output stream so far.

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeByte(0x10);
    System.out.println(os.size()); // 1

    os.writeByte(0x20);
    System.out.println(os.size()); // 2

    os.writeInt(0x12345678);
    System.out.println(os.size()); // 6
}

final var bytes = out.toByteArray();

// [10, 20, 12, 34, 56, 78]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

void write (byte[] b, int off, int len)

Writes len bytes from the specified byte array starting at offset off to the underlying output stream.

final var out = new ByteArrayOutputStream();

try (final var os = new DataOutputStream(out)) {

    final byte[] b = {10, 20, 30, 40, 50};

    os.write(b, 0, 2);
    System.out.println(Arrays.toString(out.toByteArray())); // [10, 20]

    os.write(b, 2, 3);
    System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30, 40, 50]
}

void write (int b)

Writes the specified byte (the low eight bits of the argument b) to the underlying output stream.

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.write(10);
    os.write(20);
    os.write(30);
}

System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30]

final void writeBoolean (boolean v)

Writes a boolean to the underlying output stream as a 1-byte value.

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeBoolean(true);
    os.writeBoolean(false);
}

final var bytes = out.toByteArray();

// [01, 00]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    System.out.println(is.readBoolean()); // true
    System.out.println(is.readBoolean()); // false
}

final void writeByte (int v)

Writes out a byte to the underlying output stream as a 1-byte value.

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeByte(0x10);
    os.writeByte(0x20);
    os.writeByte(0xff);
}

final var bytes = out.toByteArray();

// [10, 20, ff]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    System.out.printf("%x%n", is.readByte()); // 10
    System.out.printf("%x%n", is.readByte()); // 20
    System.out.printf("%x%n", is.readByte()); // ff
}

final void writeBytes (String s)

Writes out the string to the underlying output stream as a sequence of bytes.

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeBytes("abc");
    os.writeBytes("○△×");
}

final var bytes = out.toByteArray();

// [61, 62, 63, cb, b3, d7]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    System.out.printf("%x%n", is.readByte()); // 61
    System.out.printf("%x%n", is.readByte()); // 62
    System.out.printf("%x%n", is.readByte()); // 63
    System.out.printf("%x%n", is.readByte()); // cb
    System.out.printf("%x%n", is.readByte()); // b3
    System.out.printf("%x%n", is.readByte()); // d7
}

final void writeChar (int v)

Writes a char to the underlying output stream as a 2-byte value, high byte first.

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeChar('a');
    os.writeChar('b');
    os.writeChar('○');
    os.writeChar('△');
}

final var bytes = out.toByteArray();

// [00, 61, 00, 62, 25, cb, 25, b3]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    System.out.println(is.readChar()); // a
    System.out.println(is.readChar()); // b
    System.out.println(is.readChar()); // ○
    System.out.println(is.readChar()); // △
}

final void writeChars (String s)

Writes a string to the underlying output stream as a sequence of characters.

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeChars("abc");
    os.writeChars("○△×");
}

final var bytes = out.toByteArray();

// [00, 61, 00, 62, 00, 63, 25, cb, 25, b3, 00, d7]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    System.out.println(is.readChar()); // a
    System.out.println(is.readChar()); // b
    System.out.println(is.readChar()); // c
    System.out.println(is.readChar()); // ○
    System.out.println(is.readChar()); // △
    System.out.println(is.readChar()); // ×
}

final void writeDouble (double v)

Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the underlying output stream as an 8-byte quantity, high byte first.

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeDouble(1.23);
    os.writeDouble(-4.56);
}

final var bytes = out.toByteArray();

// [3f, f3, ae, 14, 7a, e1, 47, ae, c0, 12, 3d, 70, a3, d7, 0a, 3d]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    System.out.println(is.readDouble()); // 1.23
    System.out.println(is.readDouble()); // -4.56
}

final void writeFloat (float v)

Converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the underlying output stream as a 4-byte quantity, high byte first.

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeFloat(1.23f);
    os.writeFloat(-4.56f);
}

final var bytes = out.toByteArray();

// [3f, 9d, 70, a4, c0, 91, eb, 85]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    System.out.println(is.readFloat()); // 1.23
    System.out.println(is.readFloat()); // -4.56
}

final void writeInt (int v)

Writes an int to the underlying output stream as four bytes, high byte first.

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeInt(0x12345678);
    os.writeInt(0xff);
}

final var bytes = out.toByteArray();

// [12, 34, 56, 78, 00, 00, 00, ff]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    System.out.printf("%x%n", is.readInt()); // 12345678
    System.out.printf("%x%n", is.readInt()); // ff
}

final void writeLong (long v)

Writes a long to the underlying output stream as eight bytes, high byte first.

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeLong(0x1234567890abcdefL);
    os.writeLong(0xff);
}

final var bytes = out.toByteArray();

// [12, 34, 56, 78, 90, ab, cd, ef, 00, 00, 00, 00, 00, 00, 00, ff]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    System.out.printf("%x%n", is.readLong()); // 1234567890abcdef
    System.out.printf("%x%n", is.readLong()); // ff
}

final void writeShort (int v)

Writes a short to the underlying output stream as two bytes, high byte first.

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeShort(0x1234);
    os.writeShort(0xff);
}

final var bytes = out.toByteArray();

// [12, 34, 00, ff]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    System.out.printf("%x%n", is.readShort()); // 1234
    System.out.printf("%x%n", is.readShort()); // ff
}

final void writeUTF (String str)

Writes a string to the underlying output stream using modified UTF-8 encoding in a machine-independent manner.

final var out = new ByteArrayOutputStream();
try (final var os = new DataOutputStream(out)) {
    os.writeUTF("abc");
    os.writeUTF("○△×");
}

final var bytes = out.toByteArray();

// [00, 03, 61, 62, 63, 00, 08, e2, 97, 8b, e2, 96, b3, c3, 97]
System.out.println("[" + HexFormat.ofDelimiter(", ").formatHex(bytes) + "]");

try (final var is = new DataInputStream(new ByteArrayInputStream(bytes))) {
    System.out.println(is.readUTF()); // abc
    System.out.println(is.readUTF()); // ○△×
}

Methods declared in FilterOutputStream

close, write

Please see the link below.

Methods declared in OutputStream

nullOutputStream

Please see the link below.

Methods declared in DataOutput

write

Please see the link below.


Related posts

To top of page