Java : Flushable with Examples
Flushable (Java SE 20 & JDK 20) API Examples.
You will find code examples on most Flushable methods.
Summary
A Flushable is a destination of data that can be flushed. The flush method is invoked to write any buffered output to the underlying stream.
final var out = new ByteArrayOutputStream();
try (final var os = new BufferedOutputStream(out)) {
final Flushable flushable = os;
os.write(10);
System.out.println(Arrays.toString(out.toByteArray())); // []
flushable.flush();
System.out.println(Arrays.toString(out.toByteArray())); // [10]
os.write(20);
System.out.println(Arrays.toString(out.toByteArray())); // [10]
flushable.flush();
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20]
os.write(30);
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20]
flushable.flush();
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30]
}
Methods
void flush ()
Flushes this stream by writing any buffered output to the underlying stream.
final var out = new ByteArrayOutputStream();
try (final var os = new BufferedOutputStream(out)) {
final Flushable flushable = os;
os.write(10);
System.out.println(Arrays.toString(out.toByteArray())); // []
flushable.flush();
System.out.println(Arrays.toString(out.toByteArray())); // [10]
os.write(20);
System.out.println(Arrays.toString(out.toByteArray())); // [10]
flushable.flush();
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20]
os.write(30);
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20]
flushable.flush();
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30]
}
Related posts
- API Examples