Java : PrintWriter - API使用例
PrintWriter (Java SE 22 & JDK 22) の使い方まとめです。
ほとんどのメソッドにサンプルコードがあります。
API仕様書のおともにどうぞ。
概要
PrintWriterクラスは
- boolean, int, float といった基本データ型
- 文字列
- オブジェクト
を便利に書き出すメソッドを持ちます。
また、PrintWriterクラスは、一部のコンストラクタを除き、IOException を発生させることはありません。
代わりに checkErrorメソッドでエラーを確認します。
final var sw = new StringWriter();
try (final var writer = new PrintWriter(sw)) {
writer.println("abc");
writer.printf("num = %d", 1234);
}
System.out.println(sw);
// 結果
// ↓
//abc
//num = 1234
フィールド
protected Writer out
protected です。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
Writerで宣言されたフィールド
lock
「Java API 使用例 : Writer」をご参照ください。
コンストラクタ
PrintWriter (File file)
final var file = Path.of("R:", "java-work", "sample.txt");
try (final var writer = new PrintWriter(file.toFile())) {
writer.println("abc");
writer.println(1234);
writer.println("○△×");
}
System.out.print(Files.readString(file));
// 結果
// ↓
//abc
//1234
//○△×
PrintWriter (File file, String csn)
System.out.println("default charset : " + Charset.defaultCharset());
final var file = Path.of("R:", "java-work", "sample.txt");
final var csn = "Shift_JIS";
System.out.println("-- write --");
try (final var writer = new PrintWriter(file.toFile(), csn)) {
writer.println("abc");
writer.println(1234);
writer.println("○△×");
}
System.out.print(Files.readString(file, Charset.forName(csn)));
// 結果
// ↓
//default charset : UTF-8
//-- write --
//abc
//1234
//○△×
PrintWriter (File file, Charset charset)
System.out.println("default charset : " + Charset.defaultCharset());
final var file = Path.of("R:", "java-work", "sample.txt");
final var charset = Charset.forName("Shift_JIS");
System.out.println("-- write --");
try (final var writer = new PrintWriter(file.toFile(), charset)) {
writer.println("abc");
writer.println(1234);
writer.println("○△×");
}
System.out.print(Files.readString(file, charset));
// 結果
// ↓
//default charset : UTF-8
//-- write --
//abc
//1234
//○△×
PrintWriter (OutputStream out)
final var out = new ByteArrayOutputStream();
try (final var writer = new PrintWriter(out)) {
writer.println("abc");
writer.println(1234);
}
System.out.println(out);
// 結果
// ↓
//abc
//1234
PrintWriter (OutputStream out, boolean autoFlush)
// autoFlush = false
final var out = new ByteArrayOutputStream();
try (final var writer = new PrintWriter(out, false)) {
writer.print("abc");
System.out.println(out.toString().isEmpty());
// 結果
// ↓
//true
writer.println("XYZ");
System.out.println(out.toString().isEmpty());
// 結果
// ↓
//true
writer.printf("num = %d", 1234);
System.out.println(out.toString().isEmpty());
// 結果
// ↓
//true
}
System.out.println(out);
// 結果
// ↓
//abcXYZ
//num = 1234
// autoFlush = true
final var out = new ByteArrayOutputStream();
try (final var writer = new PrintWriter(out, true)) {
writer.print("abc");
System.out.println(out.toString().isEmpty());
// 結果
// ↓
//true
writer.println("XYZ");
System.out.println(out);
// 結果
// ↓
//abcXYZ
writer.printf("num = %d", 1234);
System.out.println(out);
// 結果
// ↓
//abcXYZ
//num = 1234
}
System.out.println(out);
// 結果
// ↓
//abcXYZ
//num = 1234
PrintWriter (OutputStream out, boolean autoFlush, Charset charset)
autoFlush については、PrintWriter(OutputStream out, boolean autoFlush) の使用例に記載しました。
そちらもご参照ください。
System.out.println("default charset : " + Charset.defaultCharset());
final var out = new ByteArrayOutputStream();
final var charset = Charset.forName("Shift_JIS");
System.out.println("-- write --");
try (final var writer = new PrintWriter(out, false, charset)) {
writer.println("abc");
writer.println(1234);
writer.println("○△×");
}
System.out.print(out.toString(charset));
// 結果
// ↓
//default charset : UTF-8
//-- write --
//abc
//1234
//○△×
PrintWriter (Writer out)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.println("abc");
writer.println(1234);
writer.println("○△×");
}
System.out.print(out);
// 結果
// ↓
//abc
//1234
//○△×
PrintWriter (Writer out, boolean autoFlush)
// autoFlush = false
final var sw = new StringWriter();
try (final var out = new BufferedWriter(sw);
final var writer = new PrintWriter(out, false)) {
writer.print("abc");
System.out.println(sw.toString().isEmpty());
// 結果
// ↓
//true
writer.println("XYZ");
System.out.println(sw.toString().isEmpty());
// 結果
// ↓
//true
writer.printf("num = %d", 1234);
System.out.println(sw.toString().isEmpty());
// 結果
// ↓
//true
}
System.out.println(sw);
// 結果
// ↓
//abcXYZ
//num = 1234
// autoFlush = true
final var sw = new StringWriter();
try (final var out = new BufferedWriter(sw);
final var writer = new PrintWriter(out, true)) {
writer.print("abc");
System.out.println(sw.toString().isEmpty());
// 結果
// ↓
//true
writer.println("XYZ");
System.out.println(sw);
// 結果
// ↓
//abcXYZ
writer.printf("num = %d", 1234);
System.out.println(sw);
// 結果
// ↓
//abcXYZ
//num = 1234
}
System.out.println(sw);
// 結果
// ↓
//abcXYZ
//num = 1234
PrintWriter (String fileName)
final var file = Path.of("R:", "java-work", "sample.txt");
final var fileName = file.toString();
System.out.println("file name : " + fileName);
System.out.println("-- write --");
try (final var writer = new PrintWriter(fileName)) {
writer.println("abc");
writer.println(1234);
writer.println("○△×");
}
System.out.print(Files.readString(file));
// 結果
// ↓
//file name : R:\java-work\sample.txt
//-- write --
//abc
//1234
//○△×
PrintWriter (String fileName, String csn)
System.out.println("default charset : " + Charset.defaultCharset());
final var file = Path.of("R:", "java-work", "sample.txt");
final var fileName = file.toString();
System.out.println("file name : " + fileName);
final var csn = "Shift_JIS";
System.out.println("-- write --");
try (final var writer = new PrintWriter(fileName, csn)) {
writer.println("abc");
writer.println(1234);
writer.println("○△×");
}
System.out.print(Files.readString(file, Charset.forName(csn)));
// 結果
// ↓
//default charset : UTF-8
//file name : R:\java-work\sample.txt
//-- write --
//abc
//1234
//○△×
PrintWriter (String fileName, Charset charset)
System.out.println("default charset : " + Charset.defaultCharset());
final var file = Path.of("R:", "java-work", "sample.txt");
final var fileName = file.toString();
System.out.println("file name : " + fileName);
final var charset = Charset.forName("Shift_JIS");
System.out.println("-- write --");
try (final var writer = new PrintWriter(fileName, charset)) {
writer.println("abc");
writer.println(1234);
writer.println("○△×");
}
System.out.print(Files.readString(file, charset));
// 結果
// ↓
//default charset : UTF-8
//file name : R:\java-work\sample.txt
//-- write --
//abc
//1234
//○△×
メソッド
PrintWriter append (char c)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.append('a').append('b').append('c');
}
System.out.println(out); // abc
PrintWriter append (CharSequence csq)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.append("abc").append("-").append("XYZ");
}
System.out.println(out); // abc-XYZ
PrintWriter append (CharSequence csq, int start, int end)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
final var csq = "abcd";
writer.append(csq, 0, 1).println();
writer.append(csq, 0, 2).println();
writer.append(csq, 0, 3).println();
writer.append(csq, 0, 4).println();
}
System.out.print(out);
// 結果
// ↓
//a
//ab
//abc
//abcd
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
final var csq = "abcd";
writer.append(csq, 0, 4).println();
writer.append(csq, 1, 4).println();
writer.append(csq, 2, 4).println();
writer.append(csq, 3, 4).println();
}
System.out.print(out);
// 結果
// ↓
//abcd
//bcd
//cd
//d
boolean checkError ()
final var file = Path.of("R:", "java-work", "sample.txt");
try (final var out = Files.newBufferedWriter(file);
final var writer = new PrintWriter(out)) {
writer.println("abc");
System.out.println("error : " + writer.checkError());
// エラーを起こすために意図的にcloseします。
System.out.println("-- close --");
out.close();
writer.println("XYZ");
System.out.println("error : " + writer.checkError());
}
// 結果
// ↓
//error : false
//-- close --
//error : true
protected void clearError ()
protected です。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
void close ()
final var file = Path.of("R:", "java-work", "sample.txt");
try (final var writer = new PrintWriter(file.toFile())) {
writer.print("abc");
writer.print("XYZ");
}
System.out.println(Files.readString(file)); // abcXYZ
// try-with-resources文を使わない例です。
final var file = Path.of("R:", "java-work", "sample.txt");
final var writer = new PrintWriter(file.toFile());
try {
writer.print("abc");
writer.print("XYZ");
} finally {
writer.close();
}
System.out.println(Files.readString(file)); // abcXYZ
void flush ()
final var out = new ByteArrayOutputStream();
try (final var writer = new PrintWriter(out, false)) {
writer.print("abc");
System.out.println(out.toString().isEmpty()); // true
writer.print(123);
System.out.println(out.toString().isEmpty()); // true
writer.flush();
System.out.println(out); // abc123
}
PrintWriter format (String format, Object... args)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.format("text = %s%n", "abc");
writer.format("num = %d%n", 1234);
writer.format("X").format("Y").format("Z");
}
System.out.println(out);
// 結果
// ↓
//text = abc
//num = 1234
//XYZ
PrintWriter format (Locale l, String format, Object... args)
System.out.println("locale : " + Locale.getDefault().toLanguageTag());
System.out.println("-- write --");
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
final var time = LocalTime.of(14, 30);
writer.format("%tr%n", time);
writer.format(Locale.ENGLISH, "%tr%n", time);
}
System.out.print(out);
// 結果
// ↓
//locale : ja-JP
//-- write --
//02:30:00 午後
//02:30:00 PM
void print (boolean b)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.print(true);
writer.print(" : ");
writer.print(false);
}
System.out.println(out); // true : false
void print (char c)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.print('a');
writer.print(" : ");
writer.print('b');
}
System.out.println(out); // a : b
void print (char[] s)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.print(new char[]{'a', 'b', 'c'});
writer.print(" : ");
writer.print(new char[]{'X', 'Y', 'Z'});
}
System.out.println(out); // abc : XYZ
void print (double d)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.print(1.234);
writer.print(" : ");
writer.print(7.89e+10);
}
System.out.println(out); // 1.234 : 7.89E10
void print (float f)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.print(1.234f);
writer.print(" : ");
writer.print(7.89e+10f);
}
System.out.println(out); // 1.234 : 7.89E10
void print (int i)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.print(1234);
writer.print(" : ");
writer.print(Integer.MAX_VALUE);
}
System.out.println(out); // 1234 : 2147483647
void print (long l)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.print(1234L);
writer.print(" : ");
writer.print(Long.MAX_VALUE);
}
System.out.println(out); // 1234 : 9223372036854775807
void print (Object obj)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.print(Path.of("R:", "java-work"));
writer.print(" : ");
writer.print(LocalTime.of(14, 30));
}
System.out.println(out); // R:\java-work : 14:30
void print (String s)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.print("abc");
writer.print(" : ");
writer.print("XYZ");
}
System.out.println(out); // abc : XYZ
PrintWriter printf (String format, Object... args)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.printf("text = %s%n", "abc");
writer.printf("num = %d%n", 1234);
writer.printf("X").printf("Y").printf("Z");
}
System.out.println(out);
// 結果
// ↓
//text = abc
//num = 1234
//XYZ
PrintWriter printf (Locale l, String format, Object... args)
System.out.println("locale : " + Locale.getDefault().toLanguageTag());
System.out.println("-- write --");
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
final var time = LocalTime.of(14, 30);
writer.printf("%tr%n", time);
writer.printf(Locale.ENGLISH, "%tr%n", time);
}
System.out.print(out);
// 結果
// ↓
//locale : ja-JP
//-- write --
//02:30:00 午後
//02:30:00 PM
void println ()
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.print("abc");
writer.println();
writer.print(123);
}
System.out.println(out);
// 結果
// ↓
//abc
//123
void println (boolean x)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.println(true);
writer.println(false);
}
System.out.print(out);
// 結果
// ↓
//true
//false
void println (char x)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.println('a');
writer.println('b');
}
System.out.print(out);
// 結果
// ↓
//a
//b
void println (char[] x)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.println(new char[]{'a', 'b', 'c'});
writer.println(new char[]{'X', 'Y', 'Z'});
}
System.out.print(out);
// 結果
// ↓
//abc
//XYZ
void println (double x)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.println(1.234);
writer.println(7.89e+10);
}
System.out.print(out);
// 結果
// ↓
//1.234
//7.89E10
void println (float x)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.println(1.234f);
writer.println(7.89e+10f);
}
System.out.print(out);
// 結果
// ↓
//1.234
//7.89E10
void println (int x)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.println(1234);
writer.println(Integer.MAX_VALUE);
}
System.out.print(out);
// 結果
// ↓
//1234
//2147483647
void println (long x)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.println(1234L);
writer.println(Long.MAX_VALUE);
}
System.out.print(out);
// 結果
// ↓
//1234
//9223372036854775807
void println (Object x)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.println(Path.of("R:", "java-work"));
writer.println(LocalTime.of(14, 30));
}
System.out.print(out);
// 結果
// ↓
//R:\java-work
//14:30
void println (String x)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.println("abc");
writer.println("XYZ");
}
System.out.print(out);
// 結果
// ↓
//abc
//XYZ
protected void setError ()
protected です。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
void write (char[] buf)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.write(new char[]{'a', 'b', 'c'});
writer.write(new char[]{'X', 'Y', 'Z'});
}
System.out.println(out); // abcXYZ
void write (char[] buf, int off, int len)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
final char[] buf = {'a', 'b', 'c', 'd'};
writer.write(buf, 0, 1);
writer.println();
writer.write(buf, 0, 2);
writer.println();
writer.write(buf, 0, 3);
writer.println();
writer.write(buf, 0, 4);
writer.println();
}
System.out.print(out);
// 結果
// ↓
//a
//ab
//abc
//abcd
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
final char[] buf = {'a', 'b', 'c', 'd'};
writer.write(buf, 0, 4);
writer.println();
writer.write(buf, 1, 3);
writer.println();
writer.write(buf, 2, 2);
writer.println();
writer.write(buf, 3, 1);
writer.println();
}
System.out.print(out);
// 結果
// ↓
//abcd
//bcd
//cd
//d
void write (int c)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.write('a');
writer.write('b');
writer.write('c');
}
System.out.println(out); // abc
void write (String s)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
writer.write("abc");
writer.write("XYZ");
}
System.out.println(out); // abcXYZ
void write (String s, int off, int len)
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
final var s = "abcd";
writer.write(s, 0, 1);
writer.println();
writer.write(s, 0, 2);
writer.println();
writer.write(s, 0, 3);
writer.println();
writer.write(s, 0, 4);
writer.println();
}
System.out.print(out);
// 結果
// ↓
//a
//ab
//abc
//abcd
final var out = new StringWriter();
try (final var writer = new PrintWriter(out)) {
final var s = "abcd";
writer.write(s, 0, 4);
writer.println();
writer.write(s, 1, 3);
writer.println();
writer.write(s, 2, 2);
writer.println();
writer.write(s, 3, 1);
writer.println();
}
System.out.print(out);
// 結果
// ↓
//abcd
//bcd
//cd
//d
Writerで宣言されたメソッド
nullWriter
「Java API 使用例 : Writer」をご参照ください。