Name

PrintWriter

Synopsis

This class is a character output stream that implements a number of print( ) and println( ) methods that output textual representations of primitive values and objects. When you create a PrintWriter object, you specify a character or byte output stream that it should write its characters to and, optionally, whether the PrintWriter stream should be automatically flushed whenever println( ) is called. If you specify a byte output stream as the destination, the PrintWriter( ) constructor automatically creates the necessary OutputStreamWriter object to convert characters to bytes using the default encoding. In Java 5.0, convenience constructors allow you to specify a file (either as a file name or a File object) as the destination. You may optionally specify the name of a charset to use for character-to-byte conversion when writing to the file.

PrintWriter implements the normal write( ), flush( ), and close( ) methods all Writer subclasses define. It is more common to use the higher-level print( ) and println( ) methods, each of which converts its argument to a string before outputting it. println( ) can also terminate the line (and optionally flush the buffer) after printing its argument. In Java 5.0, you can also use the printf( ) methods (or the format( ) methods that behave identically) for formatted output. These methods behave like the format( ) method of a java.util.Formatter object that uses the PrintWriter as its destination.

The methods of ...

Get Java in a Nutshell, 5th Edition 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.