Java PrintStream and PrintWriter

The purpose of these two classes is to write formatted data to byte and character output streams using familiar format, printf, print, and println methods.

  • public class PrintStream extends FilterOutputStream implements Appendable, Closeable, Flushable
  • public class PrintWriter extends Writer implements Appendable, AutoCloseable, Closeable, Flushable

To another output stream, PrintStream adds the ability to print representations of various data values. Its methods never throw an IOException, but rather set a flag that can be tested via the checkError() method. Optionally, a PrintStream can be constructed to flush automatically whenever a byte array is written, a println method is invoked, or a newline character is written. All characters printed by a PrintStream method are converted into bytes using the default character set for the platform. To write characters rather than bytes, use PrintWriter.

PrintWriter prints formatted representations of objects to a text-output stream. It implements all print methods of PrintStream, but does not contain methods for writing raw bytes. Its methods never throw an IOException, and a client should use checkError() to discover errors. I have no idea why PrintWriter isn’t a subclass of FilterWriter, which would be nicely symmetric with PrintStream being a subclass of FilterOutputStream.


  • public void PrintStream(OutputStream out, boolean autoFlush)
  • public void PrintStream(String fileName) autoFlush assumed false
  • public void PrintWriter(OutputStream out, boolean autoFlush)
  • public void PrintWriter(String fileName) autoFlush assumed false
  • public void PrintWriter(Writer out, boolean autoFlush)

For each type T, there are new methods

  • void print(T t) prints t.toString()
  • void println(T t) prints t.toString() + EOL
  • void printf(String format, args) prints the list of args of various types T according to the format

In addition, the following methods are overridden so that they also don’t throw an IOException.

  • void write(int c) to write a character c
  • void write(char[ ] buf, int off, int len)

Tags: , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: