Java : PushbackReader with Examples
PushbackReader (Java SE 22 & JDK 22) with Examples.
You will find code examples on most PushbackReader methods.
Summary
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
Fields declared in Reader
Constructors
PushbackReader (Reader in)
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)
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 ()
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)
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 ()
try (final var reader = new PushbackReader(new StringReader("abc"))) {
// Always returns false.
System.out.println(reader.markSupported()); // false
}
int read ()
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 ()
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 ()
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)
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)
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)
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
Methods declared in Reader
Related posts
- API Examples