Java : Writer con ejemplos

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

Nota :


Summary

Clase abstracta para escribir en secuencias de caracteres. (Traducción automática)

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

El objeto utilizado para sincronizar operaciones en esta secuencia. (Traducción automática)

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

Constructors

Writer ()

Crea un nuevo escritor de flujo de personajes cuyas secciones críticas se sincronizarán en el escritor mismo. (Traducción automática)

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

Writer (Object lock)

Crea un nuevo escritor de flujo de caracteres cuyas secciones críticas se sincronizarán en el objeto dado. (Traducción automática)

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

Methods

Writer append (char c)

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

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)

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

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)

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

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

Cierra el flujo, lavándolo primero. (Traducción automática)

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

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

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

Devuelve un nuevo escritor que descarta todos los caracteres. (Traducción automática)

try (final var writer = Writer.nullWriter()) {
    // A stream discards all characters.
    writer.append("abc");
    writer.flush();
}

void write (char[] cbuf)

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

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)

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

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)

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

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)

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

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)

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

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