Java : OutputStream - API Examples

OutputStream (Java SE 17 & JDK 17) API Examples.
You will find code examples on most OutputStream methods.


Summary

This abstract class is the superclass of all classes representing an output stream of bytes. An output stream accepts output bytes and sends them to some sink.

Class diagram

final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

try (final var os = Files.newOutputStream(file)) {
    os.write(10);
    os.write(20);
    os.write(30);
}

final var bytes = Files.readAllBytes(file);
System.out.println(Arrays.toString(bytes)); // [10, 20, 30]

Constructors

OutputStream ()

Constructor for subclasses to call.

I think it's rare to create a subclass of OutputStream. Therefore, the code example is omitted.

Methods

void close ()

Closes this output stream and releases any system resources associated with this stream.

final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

try (final var os = Files.newOutputStream(file)) {
    os.write(123);
}

final var bytes = Files.readAllBytes(file);
System.out.println(Arrays.toString(bytes)); // [123]
// An example without a try-with-resources statement.
final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

final var os = Files.newOutputStream(file);
try {
    os.write(123);
} finally {
    os.close();
}

final var bytes = Files.readAllBytes(file);
System.out.println(Arrays.toString(bytes)); // [123]

void flush ()

Flushes this output stream and forces any buffered output bytes to be written out.

final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

try (final OutputStream os = new BufferedOutputStream(Files.newOutputStream(file))) {

    os.write(123);
    System.out.println(Arrays.toString(Files.readAllBytes(file))); // []

    os.flush();
    System.out.println(Arrays.toString(Files.readAllBytes(file))); // [123]
}

static OutputStream nullOutputStream ()

Returns a new OutputStream which discards all bytes.

final byte[] b = {1, 2, 3};
final var os = OutputStream.nullOutputStream();

os.write(123);
os.write(b);
os.write(b, 0, 2);
os.flush();

os.close();

//os.write(123); // IOException: Stream closed

void write (byte[] b)

Writes b.length bytes from the specified byte array to this output stream.

final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

try (final var os = Files.newOutputStream(file)) {

    os.write(new byte[]{10, 20});

    final var bytes1 = Files.readAllBytes(file);
    System.out.println(Arrays.toString(bytes1)); // [10, 20]

    os.write(new byte[]{30, 40, 50});

    final var bytes2 = Files.readAllBytes(file);
    System.out.println(Arrays.toString(bytes2)); // [10, 20, 30, 40, 50]
}

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

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

final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

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

try (final var os = Files.newOutputStream(file)) {

    os.write(b, 0, 2);

    final var bytes1 = Files.readAllBytes(file);
    System.out.println(Arrays.toString(bytes1)); // [10, 20]

    os.write(b, 2, 3);

    final var bytes2 = Files.readAllBytes(file);
    System.out.println(Arrays.toString(bytes2)); // [10, 20, 30, 40, 50]
}

abstract void write (int b)

Writes the specified byte to this output stream.

The byte to be written is the eight low-order bits of the argument b.

final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

try (final var os = Files.newOutputStream(file)) {

    os.write(1);

    final var bytes1 = Files.readAllBytes(file);
    System.out.println(Arrays.toString(bytes1)); // [1]

    os.write(127);

    final var bytes2 = Files.readAllBytes(file);
    System.out.println(Arrays.toString(bytes2)); // [1, 127]

    os.write(128);

    final var bytes3 = Files.readAllBytes(file);
    System.out.println(Arrays.toString(bytes3)); // [1, 127, -128]

    os.write(129);

    final var bytes4 = Files.readAllBytes(file);
    System.out.println(Arrays.toString(bytes4)); // [1, 127, -128, -127]

    os.write(255);

    final var bytes5 = Files.readAllBytes(file);
    System.out.println(Arrays.toString(bytes5)); // [1, 127, -128, -127, -1]

    os.write(256);

    final var bytes6 = Files.readAllBytes(file);
    System.out.println(Arrays.toString(bytes6)); // [1, 127, -128, -127, -1, 0]

    os.write(257);

    final var bytes7 = Files.readAllBytes(file);
    System.out.println(Arrays.toString(bytes7)); //[1, 127, -128, -127, -1, 0, 1]
}

Related posts

To top of page