広告

Java : Reader - API使用例

Reader (Java SE 19 & JDK 19) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。


概要

文字ストリームを読み込むための抽象クラスです。

クラス構成

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]
}

Reader は使い終わったら close が必要です。
try-with-resources文 を使い、リソースリークが起きないようにしましょう。


フィールド

protected Object lock

このストリームに対する処理の同期に使用するオブジェクトです。

protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。

コンストラクタ

Reader ()

リーダー自体でクリティカル・セクションが同期する文字ストリーム・リーダーを新しく作成します。

protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。

Reader (Object lock)

指定されたオブジェクトでクリティカル・セクションが同期する文字ストリーム・リーダーを新しく作成します。

protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。

メソッド

abstract void close ()

ストリームを閉じて、それに関連するすべてのシステム・リソースを解放します。

try-with-resources文 を使うのがおすすめです。

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]
}
// try-with-resources文を使わない例です。
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)

ストリームの現在位置にマークを設定します。

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 {
        // readAheadLimit に達してリセットしようとすると例外が発生します。
        reader.reset();
    } catch (IOException e) {
        System.out.println("IOException! : " + e.getMessage());
    }

    // 結果
    // ↓
    //IOException! : Mark invalid
}

boolean markSupported ()

このストリームがmark()オペレーションをサポートするかどうかを判定します。

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 = new URL("https://example.com/");

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

static Reader nullReader ()

文字を読み取らない新しいReaderを返します。

final var reader = Reader.nullReader();

// なにも起きません。
System.out.println(reader.read()); // -1
System.out.println(reader.ready()); // false

reader.close();

// closeの後は例外発生
//reader.read(); // IOException: Stream closed

int read ()

単一の文字を読み込みます。

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)

配列に文字を読み込みます。

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)

配列の一部に文字を読み込みます。

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)

指定された文字バッファに文字列を読み込みます。

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

このストリームが読込み可能かどうかを判定します。

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

ストリームをリセットします。

このメソッドの使用例は、mark(int readAheadLimit) にまとめて記載しました。
そちらのAPI使用例をご参照ください。

long skip (long n)

文字をスキップします。

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)

このリーダーからすべての文字を読み込み、指定されたライターに読み込まれた順番で書き込みます。

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
    }
}

関連記事

ページの先頭へ