Java : OutputStreamWriter with Examples

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


Summary

An OutputStreamWriter is a bridge from character streams to byte streams: Characters written to it are encoded into bytes using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the default charset may be accepted.

Class diagram

final var os = new ByteArrayOutputStream();

try (final var writer = new OutputStreamWriter(os)) {
    writer.write("abcd");
}

System.out.println(os); // abcd
System.out.println(Arrays.toString(os.toByteArray())); // [97, 98, 99, 100]
final var os = new ByteArrayOutputStream();

try (final var writer = new BufferedWriter(new OutputStreamWriter(os))) {
    writer.write("abcd");
    writer.newLine();
    writer.write("XYZ");
}

System.out.println(os);

// Result
// ↓
//abcd
//XYZ

Fields declared in Writer

lock

Please see the link below.

Constructors

OutputStreamWriter (OutputStream out)

Creates an OutputStreamWriter that uses the default character encoding, or where out is a PrintStream, the charset used by the print stream.

final var os = new ByteArrayOutputStream();

try (final var writer = new OutputStreamWriter(os)) {
    writer.write("abcd");
}

System.out.println(os); // abcd
System.out.println(Arrays.toString(os.toByteArray())); // [97, 98, 99, 100]

OutputStreamWriter (OutputStream out, String charsetName)

Creates an OutputStreamWriter that uses the named charset.

final var sjis = "Shift_JIS";
final var os = new ByteArrayOutputStream();

try (final var writer = new OutputStreamWriter(os, sjis)) {
    writer.write("○△×");
}

System.out.println(os.toString(sjis)); // ○△×

final var bytes = os.toByteArray();
System.out.println(Arrays.toString(bytes)); // [-127, -101, -127, -94, -127, 126]

OutputStreamWriter (OutputStream out, Charset cs)

Creates an OutputStreamWriter that uses the given charset.

final var sjis = Charset.forName("Shift_JIS");
final var os = new ByteArrayOutputStream();

try (final var writer = new OutputStreamWriter(os, sjis)) {
    writer.write("○△×");
}

System.out.println(os.toString(sjis)); // ○△×

final var bytes = os.toByteArray();
System.out.println(Arrays.toString(bytes)); // [-127, -101, -127, -94, -127, 126]

OutputStreamWriter (OutputStream out, CharsetEncoder enc)

Creates an OutputStreamWriter that uses the given charset encoder.

final var sjis = Charset.forName("Shift_JIS");
final var os = new ByteArrayOutputStream();

try (final var writer = new OutputStreamWriter(os, sjis.newEncoder())) {
    writer.write("○△×");
}

System.out.println(os.toString(sjis)); // ○△×

final var bytes = os.toByteArray();
System.out.println(Arrays.toString(bytes)); // [-127, -101, -127, -94, -127, 126]

Methods

void close ()

Closes the stream, flushing it first.

final var os = new ByteArrayOutputStream();

try (final var writer = new OutputStreamWriter(os)) {
    writer.write("abcd");
}

System.out.println(os); // abcd
System.out.println(Arrays.toString(os.toByteArray())); // [97, 98, 99, 100]
// An example without a try-with-resources statement.
final var os = new ByteArrayOutputStream();
final var writer = new OutputStreamWriter(os);
try {
    writer.write("abcd");
} finally {
    writer.close();
}

System.out.println(os); // abcd
System.out.println(Arrays.toString(os.toByteArray())); // [97, 98, 99, 100]

void flush ()

Flushes the stream.

final var os = new ByteArrayOutputStream();

try (final var writer = new OutputStreamWriter(os)) {
    writer.write("abcd");

    final var ret1 = os.toString();
    System.out.println(ret1.isEmpty()); // true

    writer.flush();

    final var ret2 = os.toString();
    System.out.println(ret2); // abcd

    writer.write("XYZ");

    final var ret3 = os.toString();
    System.out.println(ret3); // abcd
}

final var ret4 = os.toString();
System.out.println(ret4); // abcdXYZ

String getEncoding ()

Returns the name of the character encoding being used by this stream.

try (final var writer = new OutputStreamWriter(new ByteArrayOutputStream())) {
    final var ret = writer.getEncoding();
    System.out.println(ret); // UTF8
}
final var sjis = Charset.forName("Shift_JIS");

try (final var writer = new OutputStreamWriter(new ByteArrayOutputStream(), sjis)) {
    final var ret = writer.getEncoding();
    System.out.println(ret); // SJIS
}

void write (char[] cbuf, int off, int len)

Writes a portion of an array of characters.

final char[] cbuf = {'a', 'b', 'c', 'd'};

{
    final var os = new ByteArrayOutputStream();
    try (final var writer = new OutputStreamWriter(os)) {
        writer.write(cbuf, 0, 1);
    }
    System.out.println(os); // a
}
{
    final var os = new ByteArrayOutputStream();
    try (final var writer = new OutputStreamWriter(os)) {
        writer.write(cbuf, 0, 2);
    }
    System.out.println(os); // ab
}
{
    final var os = new ByteArrayOutputStream();
    try (final var writer = new OutputStreamWriter(os)) {
        writer.write(cbuf, 0, 3);
    }
    System.out.println(os); // abc
}
{
    final var os = new ByteArrayOutputStream();
    try (final var writer = new OutputStreamWriter(os)) {
        writer.write(cbuf, 0, 4);
    }
    System.out.println(os); // abcd
}
{
    final var os = new ByteArrayOutputStream();
    try (final var writer = new OutputStreamWriter(os)) {
        //writer.write(cbuf, 0, 5); // IndexOutOfBoundsException
    }
}
final char[] cbuf = {'a', 'b', 'c', 'd'};

{
    final var os = new ByteArrayOutputStream();
    try (final var writer = new OutputStreamWriter(os)) {
        writer.write(cbuf, 0, 4);
    }
    System.out.println(os); // abcd
}
{
    final var os = new ByteArrayOutputStream();
    try (final var writer = new OutputStreamWriter(os)) {
        writer.write(cbuf, 1, 3);
    }
    System.out.println(os); // bcd
}
{
    final var os = new ByteArrayOutputStream();
    try (final var writer = new OutputStreamWriter(os)) {
        writer.write(cbuf, 2, 2);
    }
    System.out.println(os); // cd
}
{
    final var os = new ByteArrayOutputStream();
    try (final var writer = new OutputStreamWriter(os)) {
        writer.write(cbuf, 3, 1);
    }
    System.out.println(os); // d
}

void write (int c)

Writes a single character.

final var os = new ByteArrayOutputStream();

try (final var writer = new OutputStreamWriter(os)) {
    writer.write('a');
    writer.write('b');
    writer.write('c');
}

System.out.println(os); // abc

void write (String str, int off, int len)

Writes a portion of a string.

final var str = "abcd";

{
    final var os = new ByteArrayOutputStream();
    try (final var writer = new OutputStreamWriter(os)) {
        writer.write(str, 0, 1);
    }
    System.out.println(os); // a
}
{
    final var os = new ByteArrayOutputStream();
    try (final var writer = new OutputStreamWriter(os)) {
        writer.write(str, 0, 2);
    }
    System.out.println(os); // ab
}
{
    final var os = new ByteArrayOutputStream();
    try (final var writer = new OutputStreamWriter(os)) {
        writer.write(str, 0, 3);
    }
    System.out.println(os); // abc
}
{
    final var os = new ByteArrayOutputStream();
    try (final var writer = new OutputStreamWriter(os)) {
        writer.write(str, 0, 4);
    }
    System.out.println(os); // abcd
}
{
    final var os = new ByteArrayOutputStream();
    try (final var writer = new OutputStreamWriter(os)) {
        //writer.write(str, 0, 5); // IndexOutOfBoundsException
    }
}
final var str = "abcd";

{
    final var os = new ByteArrayOutputStream();
    try (final var writer = new OutputStreamWriter(os)) {
        writer.write(str, 0, 4);
    }
    System.out.println(os); // abcd
}
{
    final var os = new ByteArrayOutputStream();
    try (final var writer = new OutputStreamWriter(os)) {
        writer.write(str, 1, 3);
    }
    System.out.println(os); // bcd
}
{
    final var os = new ByteArrayOutputStream();
    try (final var writer = new OutputStreamWriter(os)) {
        writer.write(str, 2, 2);
    }
    System.out.println(os); // cd
}
{
    final var os = new ByteArrayOutputStream();
    try (final var writer = new OutputStreamWriter(os)) {
        writer.write(str, 3, 1);
    }
    System.out.println(os); // d
}

Methods declared in Writer

append, append, append, nullWriter, write, write

Please see the link below.


Related posts

To top of page