PrintStream Class

Package: java.io

The PrintStream class is similar to the PrintWriter class in that it lets you write data to an output stream. PrintStream and PrintWriter have nearly identical methods. The primary difference is that PrintStream writes raw bytes in the machine’s native character format, and PrintWriter converts bytes to recognized encoding schemes. Thus, files created with PrintWriter are more compatible across different platforms than files created with PrintStream.

In general, you should use PrintWriter rather than PrintStream. However, the most common way to write console output — System.out — uses PrintStream to write data to the operator’s console. Using System.out is the only time you should use PrintStream instead of PrintWriter. Because both classes provide the same methods, though, you probably won’t be aware of the difference when you use System.out.

CrossRef.eps The PrintStream class is one of many Java I/O classes that use streams. For more information, see Streams (Overview).

Constructors

Constructor

Description

PrintStream(Output Stream out)

Creates a print stream for the specified output stream.

PrintStream(Output Stream out, boolean flush)

Creates a print stream for the specified output stream. If the second parameter is true, the buffer is automatically flushed whenever the println method is called.

Methods

Method

Description ...

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.