Java : OutputStream with Examples
OutputStream (Java SE 21 & JDK 21) with Examples.
You will find code examples on most OutputStream methods.
Summary
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 ()
I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
Methods
void close ()
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 ()
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 ()
final byte[] b = {1, 2, 3};
try (final var os = OutputStream.nullOutputStream()) {
// A stream discards all bytes.
os.write(123);
os.write(b);
os.write(b, 0, 2);
os.flush();
}
void write (byte[] 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(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)
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)
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
- API Examples
- Closeable
- DataInput
- DataOutput
- Flushable
- InputStream
- IOException
- OutputStream
- Reader
- Writer