Java IO: PrintWriter

Jakob Jenkov
Last update: 2015-09-10

The Java PrintWriter class ( enables you to write formatted data to an underlying Writer. For instance, writing int, long and other primitive data formatted as text, rather than as their byte values.

The Java PrintWriter is useful if you are generating reports (or similar) where you have to mix text and numbers. The PrintWriter class has all the same methods as the PrintStream except for the methods to write raw bytes. Being a Writer subclass the PrintWriter is intended to write text.

PrintWriter Example

Here is a simple Java PrintWriter example:

FileWriter  writer      = new FileWriter("d:\\data\\report.txt");
PrintWriter printWriter = new PrintWriter(writer);

printWriter.print((int) 123);
printWriter.print((float) 123.456);

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


This example first creates a PrintWriter instance which is connected to a FileWriter. Second, the example writes a boolean, an int and a float to the PrintWriter. Third, the example calls the advanced printf() method of the PrintWriter which can insert formatted numbers into a text string. Finally the PrintWriter is closed.

PrintWriter Constructors

The PrintWriter has a wide selection of contructors that enable you to connect it to a File, an OutputStream, or a Writer. In that way the PrintWriter is a bit different from other Writer subclasses which tend to have mostly constructors that can take other Writer instances as parameters (except for a few, like OutputStreamWriter).

print() and format()

The Java PrintWriter class contains the powerful format() and printf() methods. The two methods do exactly the same, but the name "printf" is more familiar to C-programmers. The format() and printf() methods allow you to mix text and data in very advanced ways, using a formatting string. For more information about format() and printf() see this page:

Closing a PrintWriter

When you are finished writing characters to the Java PrintWriter you should remember to close it. Closing a PrintWriter will also close the Writer instance to which the PrintWriter is writing.

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


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

FileWriter writer = new FileWriter("data/report.txt");

try(PrintWriter printWriter =
    new PrintWriter(writer)){

    printWriter.write("Hello World ");


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 FileWriter instance is not created inside the try-with-resources block. That means that the try-with-resources block will not automatically close this FileWriter instance. However, when the PrintWriter is closed it will also close the OutputStream instance it writes to, so the FileWriter instance will get closed when the PrintWriter is closed.

Jakob Jenkov

Featured Videos

Java ForkJoinPool

P2P Networks Introduction


Java Persistence
Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Next tutorial in this tutorial trail