Java : PrintStream with Examples

PrintStream (Java SE 18 & JDK 18) API Examples.
You will find code examples on most PrintStream methods.


Summary

A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently.

Class diagram

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 object is a PrintStream.

final PrintStream ps = System.out;

ps.println("XYZ");
ps.println(-123);

// Result
// ↓
//XYZ
//-123

Fields declared in FilterOutputStream

out

Please see the link below.

Constructors

PrintStream (File file)

Creates a new print stream, without automatic line flushing, with the specified 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)

Creates a new print stream, without automatic line flushing, with the specified file and charset.

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)

Creates a new print stream, without automatic line flushing, with the specified file and 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)

Creates a new print stream, without automatic line flushing, with the specified OutputStream.

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)

Creates a new print stream, with the specified OutputStream and line flushing.

// 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)

Creates a new print stream, with the specified OutputStream, line flushing, and character 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)

Creates a new print stream, with the specified OutputStream, line flushing and 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)

Creates a new print stream, without automatic line flushing, with the specified file name.

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)

Creates a new print stream, without automatic line flushing, with the specified file name and 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 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)

Creates a new print stream, without automatic line flushing, with the specified file name and 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)

Appends the specified character to this output stream.

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)

Appends the specified character sequence to this output stream.

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)

Appends a subsequence of the specified character sequence to this output stream.

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 ()

Returns the charset used in this PrintStream instance.

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 ()

Flushes the stream and checks its error state.

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 ()

Clears the internal error state of this stream.

protected. I think it's rare to create a subclass of PrintStream. Therefore, the code example is omitted.

void close ()

Closes the stream.

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 ()

Flushes the stream.

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)

Writes a formatted string to this output stream using the specified format string and arguments.

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)

Writes a formatted string to this output stream using the specified format string and arguments.

System.out.println("locale : " + Locale.getDefault());

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)

Prints a boolean value.

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)

Prints a character.

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)

Prints an array of characters.

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)

Prints a double-precision floating-point number.

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)

Prints a floating-point number.

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.8900003E10

void print (int i)

Prints an integer.

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)

Prints a long integer.

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)

Prints an object.

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)

Prints a string.

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)

A convenience method to write a formatted string to this output stream using the specified format string and arguments.

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("X").printf("Y").printf("Z");
}

System.out.println(out);

// Result
// ↓
//text = abc
//num = 1234
//XYZ

PrintStream printf (Locale l, String format, Object... args)

A convenience method to write a formatted string to this output stream using the specified format string and arguments.

System.out.println("locale : " + Locale.getDefault());

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 ()

Terminates the current line by writing the line separator string.

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)

Prints a boolean and then terminates the line.

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)

Prints a character and then terminates the line.

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)

Prints an array of characters and then terminates the line.

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)

Prints a double and then terminates the line.

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)

Prints a float and then terminates the line.

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.8900003E10

void println (int x)

Prints an integer and then terminates the line.

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)

Prints a long and then terminates the line.

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)

Prints an Object and then terminates the line.

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)

Prints a String and then terminates the line.

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 ()

Sets the error state of the stream to true.

protected. I think it's rare to create a subclass of PrintStream. Therefore, the code example is omitted.

void write (byte[] buf)

Writes all bytes from the specified byte array to this stream.

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)

Writes len bytes from the specified byte array starting at offset off to this stream.

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)

Writes the specified byte to this stream.

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)

Writes all bytes from the specified byte array to this stream.

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

nullOutputStream

Please see the link below.


Related posts

To top of page