Readers Writers  «Prev  Next»


Lesson 3 Using PrintWriter
ObjectiveFamiliarize yourself with how to use PrintWriter.

Using Java PrintWriters

There are four constructors in the PrintWriter class.
public PrintWriter(Writer out)

public PrintWriter(Writer out, boolean autoFlush)

public PrintWriter(OutputStream out)

public PrintWriter(OutputStream out,
boolean autoFlush)

The PrintWriter can either send data to an OutputStream or to another writer. If autoFlush is set to true, the PrintWriter is flushed every time the println() method is invoked.

print( ) and println( ) Methods


checkError( ) method

None of the methods of the PrintWriter class throw IOExceptions. If any exceptions are thrown, they are caught inside the class. You can check to see whether any exceptions have been thrown with checkError().
public boolean checkError()
This method returns true if an error has occurred and false if it hasn't. There's no easy way to determine exactly what the error was, though.

Other PrintWriter methods

The PrintWriter class also contains other PrintWriter methods listed below.

public void flush()

public void close()

public void write(int c)

public void write(char buf[], int offset, int length)

public void write(char buf[])

public void write(String s, int offset, int length)

public void write(String s)
The usual Writer methods like write() and flush(), though it's rare to use them.