Lesson 2 | PrintWriter and PrintStream |
Objective | Explore how to use PrintWriter in place of PrintStream. |
PrintWriter and PrintStream
PrintWriter is another class in the java.io package, which is part of Java's I/O library. This class provides convenient methods for printing all types of data with various print methods. It's similar to PrintStream, but PrintWriter is character-oriented, not byte-oriented.
PrintWriter can be used in place of PrintStream for output to the console using the System.out object.
Here's how you could use PrintWriter to output text to the console:
import java.io.PrintWriter;
public class Main {
public static void main(String[] args) {
PrintWriter writer = new PrintWriter(System.out, true);
writer.println("Hello, Java 1.1!");
writer.close();
}
}
In the above code:
- An instance of PrintWriter is created. The constructor takes two parameters: a PrintStream object (System.out in this case), and a boolean that indicates whether the PrintWriter should automatically flush its output after every invocation of println(), printf(), or format().
- The println() method is called on the PrintWriter instance to output a string to the console.
- Finally, the PrintWriter is closed using the close() method.
Remember that PrintWriter does not throw IOExceptions, it simply sets an internal flag that can be queried via the checkError() method. Consequently, you should always check for errors by calling checkError() after a group of output operations.
Also, you should always close your streams when you're finished using them. Failure to do so may lead to resource leaks. As PrintWriter is a kind of Writer, it needs to be closed after use. If you're using Java 7 or later, you can take advantage of the try-with-resources construct for automatic resource management.
Note: While Java 1.1 does have the PrintWriter class, it does not have automatic flushing upon println(), printf(), or format() calls. This feature was added in Java 1.2. So for Java 1.1, you need to manually call flush() method after println() method.
Difference between PrintWriter and PrintStream
The main difference between the PrintStream
and PrintWriter
classes is that the PrintWriter
class properly handles multi-byte and other non-ISO Latin-1 character sets.
The other, more minor, difference is that PrintWriter
only flushes the stream when println()
is invoked, not every time a newline character is seen, like in the middle of a print
call. Sun would probably like to deprecate PrintStream
and use code>PrintWriter instead, but that would break too much existing code.
The java.io.PrintWriter
class is a subclass of java.io.Writer
that provides the familiar
print()
and println()
methods. It's very similar to the java.io.PrintStream
class.
In Java 1.0, the PrintStream
class was used for text-oriented output, but it didn't handle multiple byte character sets particularly well (or really at all). In Java 1.1 and later, streams are only for byte-oriented and numeric output. Writers should be used when you want to output text.
PrintWriter Class
Although using System.out to write to the console is acceptable, its use is probably best for debugging purposes or for sample programs, such as those found in this book. For real-world programs, the recommended method of writing to the console when using Java is through a PrintWriter stream. PrintWriter is one of the character-based classes. Using a character-based class for console output makes internationalizing your program easier. PrintWriter defines several constructors. The one we will use is shown here:
PrintWriter(OutputStream outputStream, boolean flushingOn)
Here, outputStream is an object of type OutputStream, and flushingOn controls whether Java flushes the output stream every time a println( ) method (among others) is called. If flushingOn is true, flushing automatically takes place. If false, flushing is not automatic.
PrintWriter supports the print( ) and println( ) methods. Thus, you can use these methods in the same way as you used them with System.out.
If an
argument is not a simple type, the PrintWriter methods call the object's toString( ) method and then display the result.
To write to the console by using a PrintWriter, specify System.out for the output stream and automatic flushing.
For example, this line of code creates a PrintWriter that is connected to console output:
PrintWriter pw = new PrintWriter(System.out, true);