Java : PushbackReader con ejemplos

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

Nota :


Summary

Un lector de flujo de caracteres que permite que los caracteres se inserten nuevamente en el flujo. (Traducción automática)

Class diagram

try (final var reader = new PushbackReader(new StringReader("abcd"), 3)) {
    System.out.printf("%c%n", reader.read()); // a
    System.out.printf("%c%n", reader.read()); // b

    reader.unread('X');
    reader.unread('Y');
    reader.unread('Z');

    System.out.printf("%c%n", reader.read()); // Z
    System.out.printf("%c%n", reader.read()); // Y
    System.out.printf("%c%n", reader.read()); // X
    System.out.printf("%c%n", reader.read()); // c
    System.out.printf("%c%n", reader.read()); // d
    System.out.println(reader.read()); // -1
}

Fields declared in FilterReader

in

Consulte el siguiente enlace.

Fields declared in Reader

lock

Consulte el siguiente enlace.

Constructors

PushbackReader (Reader in)

Crea un nuevo lector pushback con un búfer pushback de un carácter. (Traducción automática)

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

    reader.unread('X');

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

    reader.unread('Y');

    System.out.printf("%c%n", reader.read()); // Y
    System.out.printf("%c%n", reader.read()); // c

    reader.unread('Z');

    System.out.printf("%c%n", reader.read()); // Z
    System.out.println(reader.read()); // -1
}
try (final var reader = new PushbackReader(new StringReader("abc"))) {
    reader.unread('X');
    reader.unread('Y');
} catch (IOException e) {
    System.out.println("IOException! : " + e.getMessage());
}

// Result
// ↓
//IOException! : Pushback buffer overflow

PushbackReader (Reader in, int size)

Crea un nuevo lector pushback con un buffer pushback del tamaño indicado. (Traducción automática)

try (final var reader = new PushbackReader(new StringReader("abcd"), 3)) {
    System.out.printf("%c%n", reader.read()); // a
    System.out.printf("%c%n", reader.read()); // b

    final char[] cbuf = {'X', 'Y', 'Z'};
    reader.unread(cbuf);

    System.out.printf("%c%n", reader.read()); // X
    System.out.printf("%c%n", reader.read()); // Y
    System.out.printf("%c%n", reader.read()); // Z
    System.out.printf("%c%n", reader.read()); // c
    System.out.printf("%c%n", reader.read()); // d
    System.out.println(reader.read()); // -1
}
try (final var reader = new PushbackReader(new StringReader("abc"), 3)) {
    final char[] cbuf = {'W', 'X', 'Y', 'Z'};
    reader.unread(cbuf);
} catch (IOException e) {
    System.out.println("IOException! : " + e.getMessage());
}

// Result
// ↓
//IOException! : Pushback buffer overflow

Methods

void close ()

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

try (final var reader = new PushbackReader(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
}
// An example without a try-with-resources statement.
final var reader = new PushbackReader(new StringReader("abc"));
try {
    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
} finally {
    reader.close();
}

void mark (int readAheadLimit)

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

try (final var reader = new PushbackReader(new StringReader("abc"))) {
    reader.mark(0);
} catch (IOException e) {
    System.out.println("IOException! : " + e.getMessage());
}

// Result
// ↓
//IOException! : mark/reset not supported

boolean markSupported ()

Indica si esta secuencia admite la operación mark(), lo cual no hace. (Traducción automática)

try (final var reader = new PushbackReader(new StringReader("abc"))) {
    // Always returns false.
    System.out.println(reader.markSupported()); // false
}

int read ()

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

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

    reader.unread('X');

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

    reader.unread('Y');

    System.out.printf("%c%n", reader.read()); // Y
    System.out.printf("%c%n", reader.read()); // c

    reader.unread('Z');

    System.out.printf("%c%n", reader.read()); // Z
    System.out.println(reader.read()); // -1
}
try (final var reader = new PushbackReader(new StringReader("abc"))) {
    reader.unread('X');
    reader.unread('Y');
} catch (IOException e) {
    System.out.println("IOException! : " + e.getMessage());
}

// Result
// ↓
//IOException! : Pushback buffer overflow

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 var reader = new PushbackReader(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 var reader = new PushbackReader(new StringReader("abc"))) {
    reader.reset();
} catch (IOException e) {
    System.out.println("IOException! : " + e.getMessage());
}

// Result
// ↓
//IOException! : mark/reset not supported

void unread (char[] cbuf)

Retrocede una matriz de caracteres copiándola al frente del búfer de retroceso. (Traducción automática)

try (final var reader = new PushbackReader(new StringReader("abcd"), 3)) {
    System.out.printf("%c%n", reader.read()); // a
    System.out.printf("%c%n", reader.read()); // b

    final char[] cbuf = {'X', 'Y', 'Z'};
    reader.unread(cbuf);

    System.out.printf("%c%n", reader.read()); // X
    System.out.printf("%c%n", reader.read()); // Y
    System.out.printf("%c%n", reader.read()); // Z
    System.out.printf("%c%n", reader.read()); // c
    System.out.printf("%c%n", reader.read()); // d
    System.out.println(reader.read()); // -1
}
try (final var reader = new PushbackReader(new StringReader("abc"), 3)) {
    final char[] cbuf = {'W', 'X', 'Y', 'Z'};
    reader.unread(cbuf);
} catch (IOException e) {
    System.out.println("IOException! : " + e.getMessage());
}

// Result
// ↓
//IOException! : Pushback buffer overflow

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

Retrocede una parte de una matriz de caracteres copiándola al frente del búfer de retroceso. (Traducción automática)

try (final var reader = new PushbackReader(new StringReader("abcd"), 3)) {
    final char[] cbuf = {'X', 'Y', 'Z'};

    System.out.printf("%c%n", reader.read()); // a

    reader.unread(cbuf, 0, 3);

    System.out.printf("%c%n", reader.read()); // X
    System.out.printf("%c%n", reader.read()); // Y
    System.out.printf("%c%n", reader.read()); // Z
    System.out.printf("%c%n", reader.read()); // b

    reader.unread(cbuf, 1, 2);

    System.out.printf("%c%n", reader.read()); // Y
    System.out.printf("%c%n", reader.read()); // Z
    System.out.printf("%c%n", reader.read()); // c

    reader.unread(cbuf, 2, 1);

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

void unread (int c)

Retrocede un solo carácter copiándolo al frente del búfer de retroceso. (Traducción automática)

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

    reader.unread('X');

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

    reader.unread('Y');

    System.out.printf("%c%n", reader.read()); // Y
    System.out.printf("%c%n", reader.read()); // c

    reader.unread('Z');

    System.out.printf("%c%n", reader.read()); // Z
    System.out.println(reader.read()); // -1
}
try (final var reader = new PushbackReader(new StringReader("abc"))) {
    reader.unread('X');
    reader.unread('Y');
} catch (IOException e) {
    System.out.println("IOException! : " + e.getMessage());
}

// Result
// ↓
//IOException! : Pushback buffer overflow

Methods declared in FilterReader

read, skip

Consulte el siguiente enlace.

Methods declared in Reader

nullReader, read, read, transferTo

Consulte el siguiente enlace.


Related posts

To top of page