Java : PushbackReader - API使用例
PushbackReader (Java SE 22 & JDK 22) の使い方まとめです。
ほとんどのメソッドにサンプルコードがあります。
API仕様書のおともにどうぞ。
概要
PushbackReader を使うと、文字の読み込み途中で、次に読み込む文字を割り込みで追加(プッシュ・バック)できます。
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
}
FilterReaderで宣言されたフィールド
in
「Java API 使用例 : FilterReader」をご参照ください。
Readerで宣言されたフィールド
lock
「Java API 使用例 : Reader」をご参照ください。
コンストラクタ
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());
}
// 結果
// ↓
//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());
}
// 結果
// ↓
//IOException! : Pushback buffer overflow
メソッド
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
}
// try-with-resources文を使わない例です。
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());
}
// 結果
// ↓
//IOException! : mark/reset not supported
boolean markSupported ()
try (final var reader = new PushbackReader(new StringReader("abc"))) {
// 常に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());
}
// 結果
// ↓
//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());
}
// 結果
// ↓
//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());
}
// 結果
// ↓
//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());
}
// 結果
// ↓
//IOException! : Pushback buffer overflow
FilterReaderで宣言されたメソッド
read, skip
「Java API 使用例 : FilterReader」をご参照ください。
Readerで宣言されたメソッド
nullReader, read, read, transferTo
「Java API 使用例 : Reader」をご参照ください。