広告

Java : FilterWriter - API使用例

FilterWriter (Java SE 22 & JDK 22) の使い方まとめです。
ほとんどのメソッドにサンプルコードがあります。
API仕様書のおともにどうぞ。


概要

フィルタ処理された文字ストリームのための抽象クラスです。 抽象クラスFilterWriterそのものが、格納されているストリームにすべての要求を渡すデフォルトのメソッドを提供します。 FilterWriterのサブクラスは、これらのメソッドの一部をオーバーライドし、追加のメソッドやフィールドを提供することもできます。

クラス構成

FilterWriter は、Writer オブジェクトをラップするだけのクラスです。

class MyWriter extends FilterWriter {
    MyWriter(Writer out) {
        super(out);
    }
}

final var sw = new StringWriter();
try (final var writer = new MyWriter(sw)) {
    writer.write("abcd");
    writer.write('-');
    writer.write("XYZ");
}

System.out.println(sw); // abcd-XYZ

フィールド

protected Writer out

ベースとなる文字出力ストリームです。

class MyWriter extends FilterWriter {
    MyWriter(Writer out) {
        super(out);
    }

    Writer getOut() {
        return out;
    }
}

final var sw = new StringWriter();
try (final var writer = new MyWriter(sw)) {
    System.out.println(writer.getOut() == sw); // true
}

Writerで宣言されたフィールド

lock

Java API 使用例 : Writer」をご参照ください。

コンストラクタ

FilterWriter (Writer out)

フィルタ処理された新しいライターを作成します。

class MyWriter extends FilterWriter {
    MyWriter(Writer out) {
        super(out);
    }
}

final var sw = new StringWriter();
try (final var writer = new MyWriter(sw)) {
    writer.write("abcd");
    writer.write("XYZ");
}

System.out.println(sw); // abcdXYZ

メソッド

void close ()

ストリームを最初にフラッシュして、閉じます。

class MyWriter extends FilterWriter {
    MyWriter(Writer out) {
        super(out);
    }
}

final var path = Path.of("R:", "java-work", "aaa.txt");
System.out.println(path); // R:\java-work\aaa.txt

try (final var writer = new MyWriter(Files.newBufferedWriter(path))) {
    writer.append("abcd");
}

final var str = Files.readString(path);
System.out.println(str); // abcd
class MyWriter extends FilterWriter {
    MyWriter(Writer out) {
        super(out);
    }
}

// try-with-resources文を使わない例です。
final var path = Path.of("R:", "java-work", "aaa.txt");
System.out.println(path); // R:\java-work\aaa.txt

final var writer = new MyWriter(Files.newBufferedWriter(path));
try {
    writer.append("abcd");
} finally {
    writer.close();
}

final var str = Files.readString(path);
System.out.println(str); // abcd

void flush ()

ストリームをフラッシュします。

class MyWriter extends FilterWriter {
    MyWriter(Writer out) {
        super(out);
    }
}

final var sw = new StringWriter();
try (final var writer = new MyWriter(new BufferedWriter(sw, 128))) {

    writer.write("abcdXYZ");
    System.out.println(sw.toString().isEmpty()); // true

    writer.flush();
    System.out.println(sw); // abcdXYZ
}
class MyWriter extends FilterWriter {
    MyWriter(Writer out) {
        super(out);
    }
}

final var sw = new StringWriter();
try (final var writer = new MyWriter(new BufferedWriter(sw, 4))) {

    writer.write("abcdXYZ");
    System.out.println(sw); // abcd

    writer.flush();
    System.out.println(sw); // abcdXYZ
}

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

文字の配列の一部を書き込みます。

class MyWriter extends FilterWriter {
    MyWriter(Writer out) {
        super(out);
    }
}

final char[] cbuf = {'a', 'b', 'c', 'd'};

final var sw1 = new StringWriter();
try (final var writer = new MyWriter(sw1)) {
    writer.write(cbuf, 0, 1);
}
System.out.println(sw1); // a

final var sw2 = new StringWriter();
try (final var writer = new MyWriter(sw2)) {
    writer.write(cbuf, 0, 2);
}
System.out.println(sw2); // ab

final var sw3 = new StringWriter();
try (final var writer = new MyWriter(sw3)) {
    writer.write(cbuf, 0, 3);
}
System.out.println(sw3); // abc

final var sw4 = new StringWriter();
try (final var writer = new MyWriter(sw4)) {
    writer.write(cbuf, 0, 4);
}
System.out.println(sw4); // abcd
class MyWriter extends FilterWriter {
    MyWriter(Writer out) {
        super(out);
    }
}

final char[] cbuf = {'a', 'b', 'c', 'd'};

final var sw1 = new StringWriter();
try (final var writer = new MyWriter(sw1)) {
    writer.write(cbuf, 0, 4);
}
System.out.println(sw1); // abcd

final var sw2 = new StringWriter();
try (final var writer = new MyWriter(sw2)) {
    writer.write(cbuf, 1, 3);
}
System.out.println(sw2); // bcd

final var sw3 = new StringWriter();
try (final var writer = new MyWriter(sw3)) {
    writer.write(cbuf, 2, 2);
}
System.out.println(sw3); // cd

final var sw4 = new StringWriter();
try (final var writer = new MyWriter(sw4)) {
    writer.write(cbuf, 3, 1);
}
System.out.println(sw4); // d

void write (int c)

単一の文字を書き込みます。

class MyWriter extends FilterWriter {
    MyWriter(Writer out) {
        super(out);
    }
}

final var sw = new StringWriter();
try (final var writer = new MyWriter(sw)) {
    writer.write('a');
    writer.write('b');
    writer.write('c');
    writer.write('d');
    writer.write('X');
    writer.write('Y');
    writer.write('Z');
}

System.out.println(sw); // abcdXYZ

void write (String str, int off, int len)

文字列の一部を書き込みます。

class MyWriter extends FilterWriter {
    MyWriter(Writer out) {
        super(out);
    }
}

final var str = "abcd";

final var sw1 = new StringWriter();
try (final var writer = new MyWriter(sw1)) {
    writer.write(str, 0, 1);
}
System.out.println(sw1); // a

final var sw2 = new StringWriter();
try (final var writer = new MyWriter(sw2)) {
    writer.write(str, 0, 2);
}
System.out.println(sw2); // ab

final var sw3 = new StringWriter();
try (final var writer = new MyWriter(sw3)) {
    writer.write(str, 0, 3);
}
System.out.println(sw3); // abc

final var sw4 = new StringWriter();
try (final var writer = new MyWriter(sw4)) {
    writer.write(str, 0, 4);
}
System.out.println(sw4); // abcd
class MyWriter extends FilterWriter {
    MyWriter(Writer out) {
        super(out);
    }
}
final var str = "abcd";

final var sw1 = new StringWriter();
try (final var writer = new MyWriter(sw1)) {
    writer.write(str, 0, 4);
}
System.out.println(sw1); // abcd

final var sw2 = new StringWriter();
try (final var writer = new MyWriter(sw2)) {
    writer.write(str, 1, 3);
}
System.out.println(sw2); // bcd

final var sw3 = new StringWriter();
try (final var writer = new MyWriter(sw3)) {
    writer.write(str, 2, 2);
}
System.out.println(sw3); // cd

final var sw4 = new StringWriter();
try (final var writer = new MyWriter(sw4)) {
    writer.write(str, 3, 1);
}
System.out.println(sw4); // d

Writerで宣言されたメソッド

append, append, append, nullWriter, write, write

Java API 使用例 : Writer」をご参照ください。


関連記事

ページの先頭へ