Java : Reader con ejemplos

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

Nota :


Summary

Clase abstracta para leer flujos 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

Files.writeString(path, "abcXYZ");

try (final Reader reader = Files.newBufferedReader(path)) {
    System.out.printf("%c%n", reader.read()); // a
    System.out.printf("%c%n", reader.read()); // b
    System.out.printf("%c%n", reader.read()); // c

    final var cbuf = new char[3];

    System.out.println(reader.read(cbuf)); // 3
    System.out.println(Arrays.toString(cbuf)); // [X, Y, Z]
}

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

Reader ()

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

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

Reader (Object lock)

Crea un nuevo lector 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

abstract void close ()

Cierra la transmisión y libera todos los recursos del sistema asociados con ella. (Traducción automática)

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

Files.writeString(path, "abcd");

try (final Reader reader = Files.newBufferedReader(path)) {
    final var cbuf = new char[4];

    System.out.println(reader.read(cbuf)); // 4
    System.out.println(Arrays.toString(cbuf)); // [a, b, c, d]
}
// 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

Files.writeString(path, "abcd");

final Reader reader = Files.newBufferedReader(path);
try {
    final var cbuf = new char[4];

    System.out.println(reader.read(cbuf)); // 4
    System.out.println(Arrays.toString(cbuf)); // [a, b, c, d]
} finally {
    reader.close();
}

void mark (int readAheadLimit)

Marca la posición actual en la secuencia. (Traducción automática)

try (final Reader reader = new BufferedReader(new StringReader("abcde"))) {
    System.out.printf("%c%n", reader.read()); // a
    System.out.printf("%c%n", reader.read()); // b
    System.out.printf("%c%n", reader.read()); // c

    reader.mark(3);

    System.out.printf("%c%n", reader.read()); // d
    System.out.printf("%c%n", reader.read()); // e
    System.out.println(reader.read()); // -1

    reader.reset();

    System.out.printf("%c%n", reader.read()); // d
    System.out.printf("%c%n", reader.read()); // e
    System.out.println(reader.read()); // -1

    reader.reset();
    reader.mark(2);

    System.out.printf("%c%n", reader.read()); // d
    System.out.printf("%c%n", reader.read()); // e
    System.out.println(reader.read()); // -1

    try {
        reader.reset();
    } catch (IOException e) {
        System.out.println("IOException! : " + e.getMessage());
    }

    // Result
    // ↓
    //IOException! : Mark invalid
}

boolean markSupported ()

Indica si esta secuencia admite la operación mark(). (Traducción automática)

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

Files.writeString(path, "abcd");

try (final Reader reader = Files.newBufferedReader(path)) {
    System.out.println(reader.markSupported()); // true
}
try (final Reader reader = new StringReader("abcd")) {
    System.out.println(reader.markSupported()); // true
}
final var url = URI.create("https://example.com/").toURL();

try (final Reader reader = new InputStreamReader(url.openStream())) {
    System.out.println(reader.markSupported()); // false
}

static Reader nullReader ()

Devuelve un nuevo lector que no lee ningún carácter. (Traducción automática)

try (final var reader = Reader.nullReader()) {
    System.out.println(reader.read()); // -1
    System.out.println(reader.ready()); // false
}

int read ()

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

try (final Reader reader = new StringReader("abc")) {
    System.out.printf("%c%n", reader.read()); // a
    System.out.printf("%c%n", reader.read()); // b
    System.out.printf("%c%n", reader.read()); // c
    System.out.println(reader.read()); // -1
}

int read (char[] cbuf)

Lee caracteres en una matriz. (Traducción automática)

try (final Reader reader = new StringReader("abcde")) {
    final var cbuf1 = new char[3];

    System.out.println(reader.read(cbuf1)); // 3
    System.out.println(Arrays.toString(cbuf1)); // [a, b, c]

    final var cbuf2 = new char[5];

    System.out.println(reader.read(cbuf2)); // 2
    System.out.println(Arrays.toString(cbuf2)); // [d, e,  ,  ,  ]

    final var cbuf3 = new char[2];

    System.out.println(reader.read(cbuf3)); // -1
    System.out.println(Arrays.toString(cbuf3)); // [ ,  ]
}

abstract int read (char[] cbuf, int off, int len)

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

try (final Reader reader = new StringReader("abcdefg")) {
    final var cbuf = new char[5];

    System.out.println(reader.read(cbuf, 0, 1)); // 1
    System.out.println(Arrays.toString(cbuf)); // [a,  ,  ,  ,  ]

    System.out.println(reader.read(cbuf, 0, 2)); // 2
    System.out.println(Arrays.toString(cbuf)); // [b, c,  ,  ,  ]

    System.out.println(reader.read(cbuf, 0, 3)); // 3
    System.out.println(Arrays.toString(cbuf)); // [d, e, f,  ,  ]

    System.out.println(reader.read(cbuf, 0, 4)); // 1
    System.out.println(Arrays.toString(cbuf)); // [g, e, f,  ,  ]

    System.out.println(reader.read(cbuf, 0, 5)); // -1
    System.out.println(Arrays.toString(cbuf)); // [g, e, f,  ,  ]
}
try (final Reader reader = new StringReader("abcde")) {
    final var cbuf = new char[5];

    System.out.println(reader.read(cbuf, 4, 1)); // 1
    System.out.println(Arrays.toString(cbuf)); // [ ,  ,  ,  , a]

    System.out.println(reader.read(cbuf, 3, 1)); // 1
    System.out.println(Arrays.toString(cbuf)); // [ ,  ,  , b, a]

    System.out.println(reader.read(cbuf, 2, 1)); // 1
    System.out.println(Arrays.toString(cbuf)); // [ ,  , c, b, a]

    System.out.println(reader.read(cbuf, 1, 1)); // 1
    System.out.println(Arrays.toString(cbuf)); // [ , d, c, b, a]

    System.out.println(reader.read(cbuf, 0, 1)); // 1
    System.out.println(Arrays.toString(cbuf)); // [e, d, c, b, a]
}
try (final Reader reader = new StringReader("abcdefg")) {
    final var cbuf = new char[3];

    System.out.println(reader.read(cbuf, 0, 3)); // 3
    System.out.println(Arrays.toString(cbuf)); // [a, b, c]

    //reader.read(cbuf, 0, 4); // IndexOutOfBoundsException
    //reader.read(cbuf, 3, 1); // IndexOutOfBoundsException
}

int read (CharBuffer target)

Intenta leer caracteres en el búfer de caracteres especificado. (Traducción automática)

try (final Reader reader = new StringReader("abc")) {
    final var target = CharBuffer.allocate(5);

    System.out.println(reader.read(target)); // 3
    System.out.println(Arrays.toString(target.array())); // [a, b, c,  ,  ]
}

boolean ready ()

Indica si esta secuencia está lista para ser leída. (Traducción automática)

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

Files.writeString(path, "abc");

try (final Reader reader = Files.newBufferedReader(path)) {
    System.out.println(reader.ready()); // true
    System.out.printf("%c%n", reader.read()); // a

    System.out.println(reader.ready()); // true
    System.out.printf("%c%n", reader.read()); // b

    System.out.println(reader.ready()); // true
    System.out.printf("%c%n", reader.read()); // c

    System.out.println(reader.ready()); // false
    System.out.println(reader.read()); // -1
}

void reset ()

Restablece la transmisión. (Traducción automática)

try (final Reader reader = new BufferedReader(new StringReader("abcde"))) {
    System.out.printf("%c%n", reader.read()); // a
    System.out.printf("%c%n", reader.read()); // b
    System.out.printf("%c%n", reader.read()); // c

    reader.mark(3);

    System.out.printf("%c%n", reader.read()); // d
    System.out.printf("%c%n", reader.read()); // e
    System.out.println(reader.read()); // -1

    reader.reset();

    System.out.printf("%c%n", reader.read()); // d
    System.out.printf("%c%n", reader.read()); // e
    System.out.println(reader.read()); // -1

    reader.reset();
    reader.mark(2);

    System.out.printf("%c%n", reader.read()); // d
    System.out.printf("%c%n", reader.read()); // e
    System.out.println(reader.read()); // -1

    try {
        reader.reset();
    } catch (IOException e) {
        System.out.println("IOException! : " + e.getMessage());
    }

    // Result
    // ↓
    //IOException! : Mark invalid
}

long skip (long n)

Salta caracteres. (Traducción automática)

try (final Reader reader = new StringReader("abcdefghij")) {
    System.out.printf("%c%n", reader.read()); // a

    System.out.println(reader.skip(1)); // 1
    System.out.printf("%c%n", reader.read()); // c

    System.out.println(reader.skip(2)); // 2
    System.out.printf("%c%n", reader.read()); // f

    System.out.println(reader.skip(3)); // 3
    System.out.printf("%c%n", reader.read()); // j

    System.out.println(reader.skip(1)); // 0
    System.out.println(reader.read()); // -1
}
try (final Reader reader = new StringReader("abcde")) {
    System.out.printf("%c%n", reader.read()); // a

    System.out.println(reader.skip(10)); // 4
    System.out.println(reader.read()); // -1
}

long transferTo (Writer out)

Lee todos los caracteres de este lector y escribe los caracteres en el escritor indicado en el orden en que se leen. (Traducción automática)

try (final Reader reader = new StringReader("abcde")) {
    try (final var writer = new StringWriter()) {
        System.out.println(reader.transferTo(writer)); // 5
        System.out.println(writer); // abcde
    }
}

Related posts

To top of page