Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

io.BufferedWriter: practical reference

io.BufferedWriter: practical reference: BufferedWriter buffers characters before forwarding them to a Writer; flush or close is required when downstream visibility matters.

Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime

Syntax

java.io.BufferedWriter

BufferedWriter buffers characters before forwarding them to a Writer; flush or close is required when downstream visibility matters.

Use BufferedWriter to combine many small character writes; newLine uses the platform separator and flush forwards buffered data.

flush does not close the resource, while close flushes and closes the wrapped Writer. Choose a Charset at the earlier byte boundary.

A buffer size at or below zero is invalid. An I/O failure may surface only during a later flush or close.

The example exercises the documented element in a small deterministic program and compares exact standard output.

Examples

public class Main {
    public static void main(String[] args) throws Exception {
        var sink = new java.io.StringWriter();
        try (var writer = new java.io.BufferedWriter(sink)) { writer.write("ok"); }
        System.out.println(sink);
    }
}

Result

ok

The example exercises the documented element in a small deterministic program and compares exact standard output.

Sources