Java : Writer with Examples
Writer (Java SE 21 & JDK 21) with Examples.
You will find code examples on most Writer methods.
Summary
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
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
Constructors
Writer ()
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
Writer (Object lock)
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
Methods
Writer append (char c)
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)
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)
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 ()
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 ()
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 ()
try (final var writer = Writer.nullWriter()) {
// A stream discards all characters.
writer.append("abc");
writer.flush();
}
void write (char[] cbuf)
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)
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)
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)
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)
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
- API Examples