Java : PrintStream con ejemplos
PrintStream (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de PrintStream.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {
ps.println("abc");
ps.printf("num = %d", 1234);
}
System.out.println(out);
// Result
// ↓
//abc
//num = 1234
A System.out and err objects are the PrintStream class.
final PrintStream ps = System.out;
ps.println("XYZ");
ps.println(-123);
// Result
// ↓
//XYZ
//-123
Fields declared in FilterOutputStream
Constructors
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));
// Result
// ↓
//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)));
// Result
// ↓
//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));
// Result
// ↓
//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);
// Result
// ↓
//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());
// Result
// ↓
//true
ps.println("XYZ");
System.out.println(out.toString().isEmpty());
// Result
// ↓
//true
ps.printf("num = %d", 1234);
System.out.println(out.toString().isEmpty());
// Result
// ↓
//true
}
System.out.println(out);
// Result
// ↓
//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);
// Result
// ↓
//abc
ps.println("XYZ");
System.out.println(out);
// Result
// ↓
//abcXYZ
ps.printf("num = %d", 1234);
System.out.println(out);
// Result
// ↓
//abcXYZ
//num = 1234
}
System.out.println(out);
// Result
// ↓
//abcXYZ
//num = 1234
PrintStream (OutputStream out, boolean autoFlush, String encoding)
Please see also : 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));
// Result
// ↓
//default charset : UTF-8
//-- print --
//abc
//1234
//○△×
PrintStream (OutputStream out, boolean autoFlush, Charset charset)
Please see also : 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));
// Result
// ↓
//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));
// Result
// ↓
//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)));
// Result
// ↓
//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));
// Result
// ↓
//default charset : UTF-8
//file name : R:\java-work\sample.txt
//-- print --
//abc
//1234
//○△×
Methods
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);
// Result
// ↓
//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);
// Result
// ↓
//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
// Intentionally close the out object to make an error.
out.close();
ps.println("XYZ");
System.out.println(ps.checkError()); // true
}
protected void clearError ()
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
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
// An example without a try-with-resources statement.
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);
// Result
// ↓
//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.JAPANESE, "%tr%n", time);
}
System.out.print(out);
// Result
// ↓
//locale : en-US
//-- print --
//02:30:00 PM
//02:30:00 午後
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);
// Result
// ↓
//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.JAPANESE, "%tr%n", time);
}
System.out.print(out);
// Result
// ↓
//locale : en-US
//-- print --
//02:30:00 PM
//02:30:00 午後
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);
// Result
// ↓
//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);
// Result
// ↓
//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);
// Result
// ↓
//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);
// Result
// ↓
//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);
// Result
// ↓
//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);
// Result
// ↓
//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);
// Result
// ↓
//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);
// Result
// ↓
//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);
// Result
// ↓
//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);
// Result
// ↓
//abc
//XYZ
protected void setError ()
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
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]
Methods declared in OutputStream
Related posts
- Ejemplos de API