Tech and Media Labs
This site uses cookies to improve the user experience.




Java IO: PrintStream

Jakob Jenkov
Last update: 2015-09-03

The Java PrintStream class (java.io.PrintStream) enables you to write formatted data to an underlying OutputStream. The PrintStream class can format primitive types like int, long etc. formatted as text, rather than as their byte values. That is why it is called a PrintStream, because it formats the primitive values as text - like they would look when printed to the screen (or printed to paper).

PrintStream Example

Here is a simple Java PrintStream example:

PrintStream printStream = new PrintStream(outputStream);

printStream.print(true);
printStream.print((int) 123);
printStream.print((float) 123.456);

printStream.close();

This example first creates a PrintStream which is connected to an OutputStream. Second, the example prints three primitivate values to the PrintStream. Third, the example closes the PrintStream.

The instantiation of the OutputStream which the PrintStream writes to is left out of the example for brevity. The PrintStream has a wide selection of constructors that enable you to connect it to a File, an OutputStream etc.

System.out and System.err are PrintStreams

You may be familiar with these two well-known PrintStream instances in Java: System.out and System.err . If you have every used any of these two streams, you have already used a PrintStream.

printf()

The Java PrintStream class contains the powerful format() and printf() methods (they do exactly the same, but the name "printf" is more familiar to C-programmers). These methods allow you to mix text and data in very advanced ways, using a formatting string.

Here is a simple Java printf() example:

PrintStream printStream = new PrintStream(outputStream);

printStream.printf(Locale.UK, "Text + data: %1$", 123);

printStream.close();

For more information about format() and printf() see the JavaDoc.

Closing a PrintStream

When you are finished writing data to the PrintStream you should remember to close it. Closing a PrintStream will also close the OutputStream instance to which the PrintStream is writing.

Closing a PrintStream is done by calling its close() method. Here is how closing a PrintStream looks:

printStream.close();

You can also use the try-with-resources construct introduced in Java 7. Here is how to use and close a PrintStream looks with the try-with-resources construct:

OutputStream output = new FileOutputStream("data/data.bin");

try(PrintStream printStream =
    new PrintStream(output)){

    printStream.writeInt(123);
    printStream.writeFloat(123.45F);
    printStream.writeLong(789);
}

Notice how there is no longer any explicit close() method call. The try-with-resources construct takes care of that.

Notice also that the first FileOutputStream instance is not created inside the try-with-resources block. That means that the try-with-resources block will not automatically close this FileOutputStream instance. However, when the PrintStream is closed it will also close the OutputStream instance it writes to, so the FileOutputStream instance will get closed when the PrintStream is closed.

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC