Java : PrintStream - API使用例
PrintStream (Java SE 21 & JDK 21) の使い方まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
PrintStreamクラスは
を便利に書き出すメソッドを持ちます。
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.println("abc");
ps.printf("num = %d", 1234);
}
System.out.println(out);
// 結果
// ↓
//abc
//num = 1234
システム標準出力である System.out は、PrintStream オブジェクトです。
使ったことのあるかたも多いのではないでしょうか。
final PrintStream ps = System.out;
ps.println("XYZ");
ps.println(-123);
// 結果
// ↓
//XYZ
//-123
また、PrintStreamクラスは、一部のコンストラクタを除き、IOException を発生させることはありません。
代わりに checkError メソッドでエラーを確認します。
補足
-
System.out 以外の用途で PrintStream を使うのであれば、代わりに PrintWriter を使うことも検討してみましょう。
個人的には PrintWriter を使うことをおすすめします。 -
外部サイトになりますが、PrintStream と PrintWriter の違いについての議論です。
io - Java: Difference between PrintStream and PrintWriter - Stack Overflow
FilterOutputStreamで宣言されたフィールド
out
「Java API 使用例 : FilterOutputStream」をご参照ください。
コンストラクタ
PrintStream (File file)
final var file = Path.of("R:", "java-work", "sample.txt");
try (final var ps = new PrintStream(file.toFile())) {
ps.println("abc");
ps.println(1234);
ps.println("○△×");
}
System.out.print(Files.readString(file));
// 結果
// ↓
//abc
//1234
//○△×
PrintStream (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("-- print --");
try (final var ps = new PrintStream(file.toFile(), csn)) {
ps.println("abc");
ps.println(1234);
ps.println("○△×");
}
System.out.print(Files.readString(file, Charset.forName(csn)));
// 結果
// ↓
//default charset : UTF-8
//-- print --
//abc
//1234
//○△×
PrintStream (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("-- print --");
try (final var ps = new PrintStream(file.toFile(), charset)) {
ps.println("abc");
ps.println(1234);
ps.println("○△×");
}
System.out.print(Files.readString(file, charset));
// 結果
// ↓
//default charset : UTF-8
//-- print --
//abc
//1234
//○△×
PrintStream (OutputStream out)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.println("abc");
ps.println(1234);
}
System.out.print(out);
// 結果
// ↓
//abc
//1234
PrintStream (OutputStream out, boolean autoFlush)
// autoFlush = false
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(new BufferedOutputStream(out), false)) {
ps.print("abc");
System.out.println(out.toString().isEmpty());
// 結果
// ↓
//true
ps.println("XYZ");
System.out.println(out.toString().isEmpty());
// 結果
// ↓
//true
ps.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 ps = new PrintStream(new BufferedOutputStream(out), true)) {
ps.print("abc");
System.out.println(out);
// 結果
// ↓
//abc
ps.println("XYZ");
System.out.println(out);
// 結果
// ↓
//abcXYZ
ps.printf("num = %d", 1234);
System.out.println(out);
// 結果
// ↓
//abcXYZ
//num = 1234
}
System.out.println(out);
// 結果
// ↓
//abcXYZ
//num = 1234
PrintStream (OutputStream out, boolean autoFlush, String encoding)
autoFlush については、PrintStream (OutputStream out, boolean autoFlush) の使用例に記載しました。
そちらもご参照ください。
System.out.println("default charset : " + Charset.defaultCharset());
final var out = new ByteArrayOutputStream();
final var encoding = "Shift_JIS";
System.out.println("-- print --");
try (final var ps = new PrintStream(out, false, encoding)) {
ps.println("abc");
ps.println(1234);
ps.println("○△×");
}
System.out.print(out.toString(encoding));
// 結果
// ↓
//default charset : UTF-8
//-- print --
//abc
//1234
//○△×
PrintStream (OutputStream out, boolean autoFlush, Charset charset)
autoFlush については、PrintStream (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("-- print --");
try (final var ps = new PrintStream(out, false, charset)) {
ps.println("abc");
ps.println(1234);
ps.println("○△×");
}
System.out.print(out.toString(charset));
// 結果
// ↓
//default charset : UTF-8
//-- print --
//abc
//1234
//○△×
PrintStream (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("-- print --");
try (final var ps = new PrintStream(fileName)) {
ps.println("abc");
ps.println(1234);
ps.println("○△×");
}
System.out.print(Files.readString(file));
// 結果
// ↓
//file name : R:\java-work\sample.txt
//-- print --
//abc
//1234
//○△×
PrintStream (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("-- print --");
try (final var ps = new PrintStream(fileName, csn)) {
ps.println("abc");
ps.println(1234);
ps.println("○△×");
}
System.out.print(Files.readString(file, Charset.forName(csn)));
// 結果
// ↓
//default charset : UTF-8
//file name : R:\java-work\sample.txt
//-- print --
//abc
//1234
//○△×
PrintStream (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("-- print --");
try (final var ps = new PrintStream(fileName, charset)) {
ps.println("abc");
ps.println(1234);
ps.println("○△×");
}
System.out.print(Files.readString(file, charset));
// 結果
// ↓
//default charset : UTF-8
//file name : R:\java-work\sample.txt
//-- print --
//abc
//1234
//○△×
メソッド
PrintStream append (char c)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.append('a').append('b').append('c');
}
System.out.println(out); // abc
PrintStream append (CharSequence csq)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.append("abc").append("-").append("XYZ");
}
System.out.println(out); // abc-XYZ
PrintStream append (CharSequence csq, int start, int end)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
final var csq = "abcd";
ps.append(csq, 0, 1).println();
ps.append(csq, 0, 2).println();
ps.append(csq, 0, 3).println();
ps.append(csq, 0, 4).println();
}
System.out.print(out);
// 結果
// ↓
//a
//ab
//abc
//abcd
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
final var csq = "abcd";
ps.append(csq, 0, 4).println();
ps.append(csq, 1, 4).println();
ps.append(csq, 2, 4).println();
ps.append(csq, 3, 4).println();
}
System.out.print(out);
// 結果
// ↓
//abcd
//bcd
//cd
//d
Charset charset ()
try (final var ps = new PrintStream(new ByteArrayOutputStream())) {
System.out.println(ps.charset()); // UTF-8
}
try (final var ps = new PrintStream(
new ByteArrayOutputStream(), false, "Shift_JIS")) {
System.out.println(ps.charset()); // Shift_JIS
}
boolean checkError ()
final var file = Path.of("R:", "java-work", "sample.txt");
try (final var out = Files.newOutputStream(file);
final var ps = new PrintStream(out)) {
ps.println("abc");
System.out.println(ps.checkError()); // false
// エラーを起こすために意図的にcloseします。
out.close();
ps.println("XYZ");
System.out.println(ps.checkError()); // true
}
protected void clearError ()
protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
void close ()
final var file = Path.of("R:", "java-work", "sample.txt");
try (final var ps = new PrintStream(file.toFile())) {
ps.print("abc");
ps.print("XYZ");
}
System.out.println(Files.readString(file)); // abcXYZ
// try-with-resources文を使わない例です。
final var file = Path.of("R:", "java-work", "sample.txt");
final var ps = new PrintStream(file.toFile());
try {
ps.print("abc");
ps.print("XYZ");
} finally {
ps.close();
}
System.out.println(Files.readString(file)); // abcXYZ
void flush ()
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(new BufferedOutputStream(out), false)) {
ps.print("abc");
System.out.println(out.toString().isEmpty()); // true
ps.print(123);
System.out.println(out.toString().isEmpty()); // true
ps.flush();
System.out.println(out); // abc123
}
PrintStream format (String format, Object... args)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.format("text = %s%n", "abc");
ps.format("num = %d%n", 1234);
ps.format("X").format("Y").format("Z");
}
System.out.println(out);
// 結果
// ↓
//text = abc
//num = 1234
//XYZ
PrintStream format (Locale l, String format, Object... args)
System.out.println("locale : " + Locale.getDefault().toLanguageTag());
System.out.println("-- print --");
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
final var time = LocalTime.of(14, 30);
ps.format("%tr%n", time);
ps.format(Locale.ENGLISH, "%tr%n", time);
}
System.out.print(out);
// 結果
// ↓
//locale : ja-JP
//-- print --
//02:30:00 午後
//02:30:00 PM
void print (boolean b)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.print(true);
ps.print(" : ");
ps.print(false);
}
System.out.println(out); // true : false
void print (char c)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.print('a');
ps.print(" : ");
ps.print('b');
}
System.out.println(out); // a : b
void print (char[] s)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.print(new char[]{'a', 'b', 'c'});
ps.print(" : ");
ps.print(new char[]{'X', 'Y', 'Z'});
}
System.out.println(out); // abc : XYZ
void print (double d)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.print(1.234);
ps.print(" : ");
ps.print(7.89e+10);
}
System.out.println(out); // 1.234 : 7.89E10
void print (float f)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.print(1.234f);
ps.print(" : ");
ps.print(7.89e+10f);
}
System.out.println(out); // 1.234 : 7.89E10
void print (int i)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.print(1234);
ps.print(" : ");
ps.print(Integer.MAX_VALUE);
}
System.out.println(out); // 1234 : 2147483647
void print (long l)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.print(1234L);
ps.print(" : ");
ps.print(Long.MAX_VALUE);
}
System.out.println(out); // 1234 : 9223372036854775807
void print (Object obj)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.print(Path.of("R:", "java-work"));
ps.print(" : ");
ps.print(LocalTime.of(14, 30));
}
System.out.println(out); // R:\java-work : 14:30
void print (String s)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.print("abc");
ps.print(" : ");
ps.print("XYZ");
}
System.out.println(out); // abc : XYZ
PrintStream printf (String format, Object... args)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.printf("text = %s%n", "abc");
ps.printf("num = %d%n", 1234);
ps.printf("%d", 123).printf("%s", " - ").printf("%d", 456);
}
System.out.println(out);
// 結果
// ↓
//text = abc
//num = 1234
//123 - 456
PrintStream printf (Locale l, String format, Object... args)
System.out.println("locale : " + Locale.getDefault().toLanguageTag());
System.out.println("-- print --");
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
final var time = LocalTime.of(14, 30);
ps.printf("%tr%n", time);
ps.printf(Locale.ENGLISH, "%tr%n", time);
}
System.out.print(out);
// 結果
// ↓
//locale : ja-JP
//-- print --
//02:30:00 午後
//02:30:00 PM
void println ()
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.print("abc");
ps.println();
ps.print(123);
}
System.out.println(out);
// 結果
// ↓
//abc
//123
void println (boolean x)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.println(true);
ps.println(false);
}
System.out.print(out);
// 結果
// ↓
//true
//false
void println (char x)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.println('a');
ps.println('b');
}
System.out.print(out);
// 結果
// ↓
//a
//b
void println (char[] x)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.println(new char[]{'a', 'b', 'c'});
ps.println(new char[]{'X', 'Y', 'Z'});
}
System.out.print(out);
// 結果
// ↓
//abc
//XYZ
void println (double x)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.println(1.234);
ps.println(7.89e+10);
}
System.out.print(out);
// 結果
// ↓
//1.234
//7.89E10
void println (float x)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.println(1.234f);
ps.println(7.89e+10f);
}
System.out.print(out);
// 結果
// ↓
//1.234
//7.89E10
void println (int x)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.println(1234);
ps.println(Integer.MAX_VALUE);
}
System.out.print(out);
// 結果
// ↓
//1234
//2147483647
void println (long x)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.println(1234L);
ps.println(Long.MAX_VALUE);
}
System.out.print(out);
// 結果
// ↓
//1234
//9223372036854775807
void println (Object x)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.println(Path.of("R:", "java-work"));
ps.println(LocalTime.of(14, 30));
}
System.out.print(out);
// 結果
// ↓
//R:\java-work
//14:30
void println (String x)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.println("abc");
ps.println("XYZ");
}
System.out.print(out);
// 結果
// ↓
//abc
//XYZ
protected void setError ()
protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
void write (byte[] buf)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.write(new byte[]{10, 20});
ps.write(new byte[]{30, 40, 50});
}
final var ret = out.toByteArray();
System.out.println(Arrays.toString(ret)); // [10, 20, 30, 40, 50]
void write (byte[] buf, int off, int len)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
final byte[] buf = {10, 20, 30};
ps.write(buf, 0, 1);
System.out.println(Arrays.toString(out.toByteArray())); // [10]
ps.write(buf, 0, 2);
System.out.println(Arrays.toString(out.toByteArray())); // [10, 10, 20]
ps.write(buf, 0, 3);
System.out.println(Arrays.toString(out.toByteArray())); // [10, 10, 20, 10, 20, 30]
}
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
final byte[] buf = {10, 20, 30};
ps.write(buf, 0, 3);
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30]
ps.write(buf, 1, 2);
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30, 20, 30]
ps.write(buf, 2, 1);
System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30, 20, 30, 30]
}
void write (int b)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.write(0);
ps.write(20);
ps.write(-30);
ps.write(127);
ps.write(128);
ps.write(255);
ps.write(256);
}
final var ret = out.toByteArray();
System.out.println(Arrays.toString(ret)); // [0, 20, -30, 127, -128, -1, 0]
void writeBytes (byte[] buf)
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.writeBytes(new byte[]{10, 20});
ps.writeBytes(new byte[]{30, 40, 50});
}
final var ret = out.toByteArray();
System.out.println(Arrays.toString(ret)); // [10, 20, 30, 40, 50]
OutputStreamで宣言されたメソッド
nullOutputStream
「Java API 使用例 : OutputStream」をご参照ください。