Java : StringWriter with Examples

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


Summary

A character stream that collects its output in a string buffer, which can then be used to construct a string.

Class diagram

final var writer = new StringWriter();

writer.write("abcd");
writer.write('-');
writer.write("XYZ");

System.out.println(writer); // abcd-XYZ
final var writer = new StringWriter();

final var ret = writer.append("ABCD").append('-').append("xyz");
System.out.println(ret); // ABCD-xyz

Fields declared in Writer

lock

Please see the link below.

Constructors

StringWriter ()

Create a new string writer using the default initial string-buffer size.

final var writer = new StringWriter();

writer.write("abcd");
writer.write('-');
writer.write("XYZ");

System.out.println(writer); // abcd-XYZ

StringWriter (int initialSize)

Create a new string writer using the specified initial string-buffer size.

final var writer1 = new StringWriter();
System.out.println(writer1.getBuffer().capacity()); // 16

final var writer2 = new StringWriter(128);
System.out.println(writer2.getBuffer().capacity()); // 128

Methods

StringWriter append (char c)

Appends the specified character to this writer.

final var 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

StringWriter append (CharSequence csq)

Appends the specified character sequence to this writer.

final var writer = new StringWriter();

final var ret = writer.append("abc").append("XYZ");
System.out.println(ret); // abcXYZ
final var 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

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

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

final var csq = "abcd";

System.out.println(new StringWriter().append(csq, 0, 1)); // a
System.out.println(new StringWriter().append(csq, 0, 2)); // ab
System.out.println(new StringWriter().append(csq, 0, 3)); // abc
System.out.println(new StringWriter().append(csq, 0, 4)); // abcd
//new StringWriter().append(csq, 0, 5); // StringIndexOutOfBoundsException
final var csq = "abcd";
System.out.println(new StringWriter().append(csq, 0, 4)); // abcd
System.out.println(new StringWriter().append(csq, 1, 4)); // bcd
System.out.println(new StringWriter().append(csq, 2, 4)); // cd
System.out.println(new StringWriter().append(csq, 3, 4)); // d

void close ()

Closing a StringWriter has no effect.

final var writer = new StringWriter();

// No effect.
writer.close();

writer.append("abcd");
System.out.println(writer); // abcd

void flush ()

Flush the stream.

final var writer = new StringWriter();

writer.append("abc");
System.out.println(writer);

// No effect.
writer.flush();

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

StringBuffer getBuffer ()

Return the string buffer itself.

final var writer = new StringWriter();

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

final var buffer = writer.getBuffer();
System.out.println(buffer); // abc

buffer.append(123);
buffer.append("XYZ");

System.out.println(buffer); // abc123XYZ
System.out.println(writer); // abc123XYZ

String toString ()

Return the buffer's current value as a string.

final var writer = new StringWriter();

writer.append("abc");

final var ret1 = writer.toString();
System.out.println(ret1); // abc

writer.append("XYZ");

final var ret2 = writer.toString();
System.out.println(ret2); // abcXYZ

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

Write a portion of an array of characters.

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

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

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

void write (int c)

Write a single character.

final var 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)

Write a string.

final var 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)

Write a portion of a string.

final var str = "abcd";

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

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

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

Methods declared in Writer

nullWriter, write

Please see the link below.


Related posts

To top of page