Java : StringWriter con ejemplos

StringWriter (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de StringWriter.

Nota :


Summary

Un flujo de caracteres que recoge su salida en un búfer de cadena, que luego puede utilizarse para construir una cadena. (Traducción automática)

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

Consulte el siguiente enlace.

Constructors

StringWriter ()

Crea un nuevo escritor de cadenas utilizando el tamaño de búfer de cadena inicial predeterminado. (Traducción automática)

final var writer = new StringWriter();

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

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

StringWriter (int initialSize)

Crea un nuevo escritor de cadenas utilizando el tamaño de búfer de cadena inicial especificado. (Traducción automática)

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)

Añade el carácter especificado a este escritor. (Traducción automática)

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)

Añade la secuencia de caracteres especificada a este escritor. (Traducción automática)

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)

Añade una subsecuencia de la secuencia de caracteres especificada a este escritor. (Traducción automática)

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 ()

Cerrar un StringWriter no tiene ningún efecto. (Traducción automática)

final var writer = new StringWriter();

// Closing a stream has no effect.
writer.close();

// The methods in this class can be called after the stream has been closed.
writer.append("abcd");
System.out.println(writer); // abcd

void flush ()

Limpiar la corriente. (Traducción automática)

final var writer = new StringWriter();

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

// The flush method of StringWriter does nothing.
writer.flush();

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

StringBuffer getBuffer ()

Devuelve el buffer de cadena en sí. (Traducción automática)

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 ()

Devuelve el valor actual del buffer como una cadena. (Traducción automática)

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)

Escribe una parte de una matriz de caracteres. (Traducción automática)

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)

Escribe un solo caracter. (Traducción automática)

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)

Escribe una cadena. (Traducción automática)

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)

Escribe una porción de una cadena. (Traducción automática)

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

Consulte el siguiente enlace.


Related posts

To top of page