Java : Writer - API Examples

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


Summary

Abstract class for writing to character streams.

Class diagram

final var path = Path.of("R:", "java-work", "aaa.txt");
System.out.println(path); // R:\java-work\aaa.txt
System.out.println(Files.exists(path)); // false

try (final Writer writer = Files.newBufferedWriter(path)) {

    writer.write("abcd");
    writer.write("XYZ");
}

final var str = Files.readString(path);
System.out.println(str); // abcdXYZ

Fields

protected Object lock

The object used to synchronize operations on this stream.

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

Constructors

Writer ()

Creates a new character-stream writer whose critical sections will synchronize on the writer itself.

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

Writer (Object lock)

Creates a new character-stream writer whose critical sections will synchronize on the given object.

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

Methods

Writer append (char c)

Appends the specified character to this writer.

try (final Writer writer = new StringWriter()) {

    System.out.println(writer.append('a')); // a
    System.out.println(writer.append('b')); // ab
    System.out.println(writer.append('X').append('Y').append('Z')); // abXYX
}

Writer append (CharSequence csq)

Appends the specified character sequence to this writer.

try (final Writer writer = new StringWriter()) {
    final var ret = writer.append("abc").append("XYZ");
    System.out.println(ret); // abcXYZ
}
try (final Writer writer = new StringWriter()) {

    final CharSequence csq1 = "abc";

    System.out.println(writer.append(csq1)); // abc

    final var sb = new StringBuilder();
    final CharSequence csq2 = sb;

    sb.append("XY");
    System.out.println(writer.append(csq2)); // abcXY

    sb.append("Z");
    System.out.println(writer.append(csq2)); // abcXYXYZ
}

Writer append (CharSequence csq, int start, int end)

Appends a subsequence of the specified character sequence to this writer.

final CharSequence csq = "abcd";

try (final Writer writer = new StringWriter()) {
    System.out.println(writer.append(csq, 0, 1)); // a
}

try (final Writer writer = new StringWriter()) {
    System.out.println(writer.append(csq, 0, 2)); // ab
}

try (final Writer writer = new StringWriter()) {
    System.out.println(writer.append(csq, 0, 3)); // abc
}

try (final Writer writer = new StringWriter()) {
    System.out.println(writer.append(csq, 0, 4)); // abcd
}

try (final Writer writer = new StringWriter()) {
    //writer.append(csq, 0, 5); // StringIndexOutOfBoundsException
}
final CharSequence csq = "abcd";

try (final Writer writer = new StringWriter()) {
    System.out.println(writer.append(csq, 0, 4)); // abcd
}

try (final Writer writer = new StringWriter()) {
    System.out.println(writer.append(csq, 1, 4)); // bcd
}

try (final Writer writer = new StringWriter()) {
    System.out.println(writer.append(csq, 2, 4)); // cd
}

try (final Writer writer = new StringWriter()) {
    System.out.println(writer.append(csq, 3, 4)); // d
}

abstract void close ()

Closes the stream, flushing it first.

final var path = Path.of("R:", "java-work", "aaa.txt");
System.out.println(path); // R:\java-work\aaa.txt

try (final Writer writer = Files.newBufferedWriter(path)) {
    writer.append("abcd");
}

final var str = Files.readString(path);
System.out.println(str); // abcd
// An example without a try-with-resources statement.
final var path = Path.of("R:", "java-work", "aaa.txt");
System.out.println(path); // R:\java-work\aaa.txt

final Writer writer = Files.newBufferedWriter(path);
try {
    writer.append("abcd");
} finally {
    writer.close();
}

final var str = Files.readString(path);
System.out.println(str); // abcd

abstract void flush ()

Flushes the stream.

final var path = Path.of("R:", "java-work", "aaa.txt");
System.out.println(path); // R:\java-work\aaa.txt

try (final Writer writer = Files.newBufferedWriter(path)) {
    writer.append("abcd");
    System.out.println(Files.readString(path).isEmpty()); // true

    writer.flush();
    System.out.println(Files.readString(path)); // abcd
}

static Writer nullWriter ()

Returns a new Writer which discards all characters.

final var writer = Writer.nullWriter();

writer.append("abc");
writer.flush();

writer.close();

//writer.append("abc"); // IOException: Stream closed

void write (char[] cbuf)

Writes an array of characters.

try (final Writer writer = new StringWriter()) {

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

    writer.write(cbuf1);
    System.out.println(writer); // abc

    final char[] cbuf2 = {'X', 'Y', 'Z'};

    writer.write(cbuf2);
    System.out.println(writer); // abcXYZ
}

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

Writes a portion of an array of characters.

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

try (final Writer writer = new StringWriter()) {
    writer.write(cbuf, 0, 1);
    System.out.println(writer); // a
}

try (final Writer writer = new StringWriter()) {
    writer.write(cbuf, 0, 2);
    System.out.println(writer); // ab
}

try (final Writer writer = new StringWriter()) {
    writer.write(cbuf, 0, 3);
    System.out.println(writer); // abc
}

try (final Writer writer = new StringWriter()) {
    writer.write(cbuf, 0, 4);
    System.out.println(writer); // abcd
}

try (final Writer writer = new StringWriter()) {
    //writer.write(cbuf, 0, 5); // IndexOutOfBoundsException
}
final char[] cbuf = {'a', 'b', 'c', 'd'};

try (final Writer writer = new StringWriter()) {
    writer.write(cbuf, 0, 4);
    System.out.println(writer); // abcd
}

try (final Writer writer = new StringWriter()) {
    writer.write(cbuf, 1, 3);
    System.out.println(writer); // bcd
}

try (final Writer writer = new StringWriter()) {
    writer.write(cbuf, 2, 2);
    System.out.println(writer); // cd
}

try (final Writer writer = new StringWriter()) {
    writer.write(cbuf, 3, 1);
    System.out.println(writer); // d
}

void write (int c)

Writes a single character.

try (final Writer writer = new StringWriter()) {

    writer.write('a');
    System.out.println(writer); // a

    writer.write('b');
    System.out.println(writer); // ab

    writer.write('X');
    System.out.println(writer); // abX

    writer.write('Y');
    System.out.println(writer); // abXY

    writer.write('Z');
    System.out.println(writer); // abXYZ
}

void write (String str)

Writes a string.

try (final Writer writer = new StringWriter()) {

    writer.write("abc");
    System.out.println(writer); // abc

    writer.write("XYZ");
    System.out.println(writer); // abcXYZ
}

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

Writes a portion of a string.

final var str = "abcd";

try (final Writer writer = new StringWriter()) {
    writer.write(str, 0, 1);
    System.out.println(writer); // a
}

try (final Writer writer = new StringWriter()) {
    writer.write(str, 0, 2);
    System.out.println(writer); // ab
}

try (final Writer writer = new StringWriter()) {
    writer.write(str, 0, 3);
    System.out.println(writer); // abc
}

try (final Writer writer = new StringWriter()) {
    writer.write(str, 0, 4);
    System.out.println(writer); // abcd
}

try (final Writer writer = new StringWriter()) {
    //writer.write(str, 0, 5); // IndexOutOfBoundsException
}
final var str = "abcd";

try (final Writer writer = new StringWriter()) {
    writer.write(str, 0, 4);
    System.out.println(writer); // abcd
}

try (final Writer writer = new StringWriter()) {
    writer.write(str, 1, 3);
    System.out.println(writer); // bcd
}

try (final Writer writer = new StringWriter()) {
    writer.write(str, 2, 2);
    System.out.println(writer); // cd
}

try (final Writer writer = new StringWriter()) {
    writer.write(str, 3, 1);
    System.out.println(writer); // d
}

Related posts

To top of page