Team LiB
Previous Section Next Section

Java 1.1appendable closeable flushable

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 PrintWriter never throw exceptions. Instead, when errors occur, they set an internal flag you can check by calling checkError( ). checkError( ) first flushes the internal stream and then returns true if any exception has occurred while writing values to that stream. Once an error has occurred on a PrintWriter object, all subsequent calls to checkError( ) return true; there is no way to reset the error flag.

PrintWriter is the character stream analog to PrintStream, which it supersedes. You can usually easily replace any PrintStream objects in a program with PrintWriter objects. This is particularly important for internationalized programs. The only valid remaining use for the PrintStream class is for the System.out and System.err standard output streams. See PrintStream for details.

Figure 9-50.

public class PrintWriter extends Writer {
// Public Constructors
5.0  public PrintWriter(String fileName) throws FileNotFoundException;  
5.0  public PrintWriter(File file) throws FileNotFoundException;  
     public PrintWriter(OutputStream out);  
     public PrintWriter(Writer out);  
5.0  public PrintWriter(File file, String csn) throws FileNotFoundException, 
5.0  public PrintWriter(String fileName, String csn) throws FileNotFoundException, 
     public PrintWriter(OutputStream out, boolean autoFlush);  
     public PrintWriter(Writer out, boolean autoFlush);  
// Public Instance Methods
5.0  public PrintWriter append(char c);  
5.0  public PrintWriter append(CharSequence csq);  
5.0  public PrintWriter append(CharSequence csq, int start, int end);  
     public boolean checkError( );  
5.0  public PrintWriter format(String format, Object... args);  
5.0  public PrintWriter format(java.util.Locale l, String format, Object... args);  
     public void print(double d);  
     public void print(float f);  
     public void print(long l);  
     public void print(Object obj);  
     public void print(String s);  
     public void print(char[ ] s);  
     public void print(boolean b);  
     public void print(char c);  
     public void print(int i);  
5.0  public PrintWriter printf(String format, Object... args);  
5.0  public PrintWriter printf(java.util.Locale l, String format, Object... args);  
     public void println( );  
     public void println(double x);  
     public void println(float x);  
     public void println(char[ ] x);  
     public void println(Object x);  
     public void println(String x);  
     public void println(char x);  
     public void println(boolean x);  
     public void println(long x);  
     public void println(int x);  
// Public Methods Overriding Writer
     public void close( );  
     public void flush( );  
     public void write(String s);  
     public void write(char[ ] buf);  
     public void write(int c);  
     public void write(String s, int off, int len);  
     public void write(char[ ] buf, int off, int len);  
// Protected Instance Methods
     protected void setError( );  
// Protected Instance Fields
1.2  protected Writer out;  

Passed To

THRowable.printStackTrace( ), java.util.Properties.list( ), javax.xml.transform.TransformerException.printStackTrace( ), javax.xml.xpath.XPathException.printStackTrace( )

    Team LiB
    Previous Section Next Section