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
.
Constructors
Constructor |
Description |
|
Creates a print stream for the specified output stream. |
|
Creates a print stream for the specified output stream. If the second parameter is |
Methods
Method |
Description |
|
Closes the file. |
|
Writes the contents of the buffer to the hard drive. |
|
Writes the value, which can be any primitive type or any object. If the value is an object, the object’s |
|
Writes the value, which can be any primitive type or any object. If the value is an object, the object’s |
Writing to PrintStream
The most common way to write data to a PrintStream
is with the println
method, which writes a complete line of text. For example:
System.out.println(“Good Morning!”);