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

void close()

Closes the file.

void flush()

Writes the contents of the buffer to the hard drive.

void print( value)

Writes the value, which can be any primitive type or any object. If the value is an object, the object’s toString() method is called.

void println( value)

Writes the value, which can be any primitive type or any object. If the value is an object, the object’s toString() method is called. A line break is written following the value.

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!”);

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset