Java : PushbackReader with Examples

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


Summary

A character-stream reader that allows characters to be pushed back into the stream.

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

Please see the link below.

Fields declared in Reader

lock

Please see the link below.

Constructors

PushbackReader (Reader in)

Creates a new pushback reader with a one-character pushback buffer.

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)

Creates a new pushback reader with a pushback buffer of the given size.

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

Closes the stream and releases any system resources associated with it.

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)

Marks the present position in the stream.

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

Tells whether this stream supports the mark() operation, which it does not.

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

int read ()

Reads a single character.

Please see PushbackReader(Reader in).

boolean ready ()

Tells whether this stream is ready to be read.

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

Resets the stream.

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)

Pushes back an array of characters by copying it to the front of the pushback buffer.

Please see PushbackReader(Reader in, int size).

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

Pushes back a portion of an array of characters by copying it to the front of the pushback buffer.

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)

Pushes back a single character by copying it to the front of the pushback buffer.

Please see PushbackReader(Reader in).

Methods declared in FilterReader

read, skip

Please see the link below.

Methods declared in Reader

nullReader, read, read, transferTo

Please see the link below.


Related posts

To top of page