Java : BufferedWriter - API使用例
BufferedWriter (Java SE 21 & JDK 21) の使い方まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
BufferedWriter は使い終わったら close が必要です。
try-with-resources文 を使い、リソースリークが起きないようにしましょう。
final var path = Path.of("R:", "java-work", "aaa.txt");
System.out.println(path); // R:\java-work\aaa.txt
System.out.println(Files.notExists(path)); // true
try (final var writer = Files.newBufferedWriter(path)) {
writer.write("abcd");
writer.newLine();
writer.write("XYZ");
writer.newLine();
writer.write("12345");
writer.newLine();
}
final var str = Files.readString(path);
System.out.println(str);
// 結果
// ↓
//abcd
//XYZ
//12345
Writerで宣言されたフィールド
lock
「Java API 使用例 : Writer」をご参照ください。
コンストラクタ
BufferedWriter (Writer out)
final var sw = new StringWriter();
try (final var writer = new BufferedWriter(sw)) {
writer.write("abcd");
writer.write("XYZ");
}
System.out.println(sw); // abcdXYZ
BufferedWriter (Writer out, int sz)
final var sw = new StringWriter();
try (final var writer = new BufferedWriter(sw, 128)) {
writer.write("abcd");
writer.write("XYZ");
}
System.out.println(sw); // abcdXYZ
メソッド
void close ()
final var path = Path.of("R:", "java-work", "aaa.txt");
System.out.println(path); // R:\java-work\aaa.txt
try (final var writer = Files.newBufferedWriter(path)) {
writer.append("abcd");
}
final var str = Files.readString(path);
System.out.println(str); // abcd
// 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 = Files.newBufferedWriter(path);
try {
writer.append("abcd");
} finally {
writer.close();
}
final var str = Files.readString(path);
System.out.println(str); // abcd
void flush ()
final var sw = new StringWriter();
try (final var writer = new BufferedWriter(sw, 128)) {
writer.write("abcdXYZ");
System.out.println(sw.toString().isEmpty()); // true
writer.flush();
System.out.println(sw); // abcdXYZ
}
final var sw = new StringWriter();
try (final var writer = new BufferedWriter(sw, 4)) {
writer.write("abcdXYZ");
System.out.println(sw); // abcd
writer.flush();
System.out.println(sw); // abcdXYZ
}
void newLine ()
final var path = Path.of("R:", "java-work", "aaa.txt");
System.out.println(path); // R:\java-work\aaa.txt
try (final var writer = Files.newBufferedWriter(path)) {
writer.write("abcd");
writer.write("XYZ");
writer.newLine();
writer.write("12345");
}
final var str = Files.readString(path);
System.out.println(str);
// 結果
// ↓
//abcdXYZ
//12345
void write (char[] cbuf, int off, int len)
final char[] cbuf = {'a', 'b', 'c', 'd'};
final var sw1 = new StringWriter();
try (final var writer = new BufferedWriter(sw1)) {
writer.write(cbuf, 0, 1);
}
System.out.println(sw1); // a
final var sw2 = new StringWriter();
try (final var writer = new BufferedWriter(sw2)) {
writer.write(cbuf, 0, 2);
}
System.out.println(sw2); // ab
final var sw3 = new StringWriter();
try (final var writer = new BufferedWriter(sw3)) {
writer.write(cbuf, 0, 3);
}
System.out.println(sw3); // abc
final var sw4 = new StringWriter();
try (final var writer = new BufferedWriter(sw4)) {
writer.write(cbuf, 0, 4);
}
System.out.println(sw4); // abcd
final char[] cbuf = {'a', 'b', 'c', 'd'};
final var sw1 = new StringWriter();
try (final var writer = new BufferedWriter(sw1)) {
writer.write(cbuf, 0, 4);
}
System.out.println(sw1); // abcd
final var sw2 = new StringWriter();
try (final var writer = new BufferedWriter(sw2)) {
writer.write(cbuf, 1, 3);
}
System.out.println(sw2); // bcd
final var sw3 = new StringWriter();
try (final var writer = new BufferedWriter(sw3)) {
writer.write(cbuf, 2, 2);
}
System.out.println(sw3); // cd
final var sw4 = new StringWriter();
try (final var writer = new BufferedWriter(sw4)) {
writer.write(cbuf, 3, 1);
}
System.out.println(sw4); // d
void write (int c)
final var sw = new StringWriter();
try (final var writer = new BufferedWriter(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 s, int off, int len)
final var str = "abcd";
final var sw1 = new StringWriter();
try (final var writer = new BufferedWriter(sw1)) {
writer.write(str, 0, 1);
}
System.out.println(sw1); // a
final var sw2 = new StringWriter();
try (final var writer = new BufferedWriter(sw2)) {
writer.write(str, 0, 2);
}
System.out.println(sw2); // ab
final var sw3 = new StringWriter();
try (final var writer = new BufferedWriter(sw3)) {
writer.write(str, 0, 3);
}
System.out.println(sw3); // abc
final var sw4 = new StringWriter();
try (final var writer = new BufferedWriter(sw4)) {
writer.write(str, 0, 4);
}
System.out.println(sw4); // abcd
final var str = "abcd";
final var sw1 = new StringWriter();
try (final var writer = new BufferedWriter(sw1)) {
writer.write(str, 0, 4);
}
System.out.println(sw1); // abcd
final var sw2 = new StringWriter();
try (final var writer = new BufferedWriter(sw2)) {
writer.write(str, 1, 3);
}
System.out.println(sw2); // bcd
final var sw3 = new StringWriter();
try (final var writer = new BufferedWriter(sw3)) {
writer.write(str, 2, 2);
}
System.out.println(sw3); // cd
final var sw4 = new StringWriter();
try (final var writer = new BufferedWriter(sw4)) {
writer.write(str, 3, 1);
}
System.out.println(sw4); // d
Writerで宣言されたメソッド
append, append, append, nullWriter, write, write
「Java API 使用例 : Writer」をご参照ください。