java.io

Class PrintWriter

public class PrintWriter extends Writer

This class prints Java primitive values and objects to a stream as text. None of the methods in this class throw an exception. However, errors can be detected by calling the checkError() method. Additionally, this stream can be designated as "autoflush" when created so that any writes are automatically flushed to the underlying output sink whenever one of the println methods is called. (Note that this differs from the PrintStream class which also auto-flushes when it encounters a newline character in the chars written).

UNKNOWN: April 17, 1998.

Field Summary
protected Writerout
This is the underlying Writer we are sending output to
Constructor Summary
PrintWriter(Writer wr)
This method intializes a new PrintWriter object to write to the specified output sink.
PrintWriter(Writer wr, boolean autoflush)
This method intializes a new PrintWriter object to write to the specified output sink.
PrintWriter(OutputStream out)
This method initializes a new PrintWriter object to write to the specified OutputStream.
PrintWriter(OutputStream out, boolean autoflush)
This method initializes a new PrintWriter object to write to the specified OutputStream.
PrintWriter(String file)
This initializes a new PrintWriter object to write to the specified file.
PrintWriter(String file, String enc)
This initializes a new PrintWriter object to write to the specified file.
PrintWriter(File file)
This initializes a new PrintWriter object to write to the specified file.
PrintWriter(File file, String enc)
This initializes a new PrintWriter object to write to the specified file.
Method Summary
PrintWriterappend(char c)
PrintWriterappend(CharSequence cs)
PrintWriterappend(CharSequence cs, int start, int end)
booleancheckError()
This method checks to see if an error has occurred on this stream.
voidclose()
This method closes this stream and all underlying streams.
voidflush()
This method flushes any buffered chars to the underlying stream and then flushes that stream as well.
PrintWriterformat(String format, Object... args)
PrintWriterformat(Locale locale, String format, Object... args)
voidprint(String str)
This method prints a String to the stream.
voidprint(char ch)
This method prints a char to the stream.
voidprint(char[] charArray)
This method prints an array of characters to the stream.
voidprint(boolean bool)
This methods prints a boolean value to the stream.
voidprint(int inum)
This method prints an integer to the stream.
voidprint(long lnum)
This method prints a long to the stream.
voidprint(float fnum)
This method prints a float to the stream.
voidprint(double dnum)
This method prints a double to the stream.
voidprint(Object obj)
This method prints an Object to the stream.
PrintWriterprintf(String format, Object... args)
PrintWriterprintf(Locale locale, String format, Object... args)
voidprintln()
This method prints a line separator sequence to the stream.
voidprintln(boolean bool)
This methods prints a boolean value to the stream.
voidprintln(int inum)
This method prints an integer to the stream.
voidprintln(long lnum)
This method prints a long to the stream.
voidprintln(float fnum)
This method prints a float to the stream.
voidprintln(double dnum)
This method prints a double to the stream.
voidprintln(Object obj)
This method prints an Object to the stream.
voidprintln(String str)
This method prints a String to the stream.
voidprintln(char ch)
This method prints a char to the stream.
voidprintln(char[] charArray)
This method prints an array of characters to the stream.
protected voidsetError()
This method can be called by subclasses to indicate that an error has occurred and should be reported by checkError.
voidwrite(int ch)
This method writes a single char to the stream.
voidwrite(char[] charArray, int offset, int count)
This method writes count chars from the specified array starting at index offset into the array.
voidwrite(String str, int offset, int count)
This method writes count chars from the specified String to the output starting at character position offset into the String
voidwrite(char[] charArray)
This method write all the chars in the specified array to the output.
voidwrite(String str)
This method writes the contents of the specified String to the underlying stream.

Field Detail

out

protected Writer out
This is the underlying Writer we are sending output to

Constructor Detail

PrintWriter

public PrintWriter(Writer wr)
This method intializes a new PrintWriter object to write to the specified output sink. The form of the constructor does not enable auto-flush functionality.

Parameters: wr The Writer to write to.

PrintWriter

public PrintWriter(Writer wr, boolean autoflush)
This method intializes a new PrintWriter object to write to the specified output sink. This constructor also allows "auto-flush" functionality to be specified where the stream will be flushed after every line is terminated or newline character is written.

Parameters: wr The Writer to write to. autoflush true to flush the stream after every line, false otherwise

PrintWriter

public PrintWriter(OutputStream out)
This method initializes a new PrintWriter object to write to the specified OutputStream. Characters will be converted to chars using the system default encoding. Auto-flush functionality will not be enabled.

Parameters: out The OutputStream to write to

PrintWriter

public PrintWriter(OutputStream out, boolean autoflush)
This method initializes a new PrintWriter object to write to the specified OutputStream. Characters will be converted to chars using the system default encoding. This form of the constructor allows auto-flush functionality to be enabled if desired

Parameters: out The OutputStream to write to autoflush true to flush the stream after every println call, false otherwise.

PrintWriter

public PrintWriter(String file)
This initializes a new PrintWriter object to write to the specified file. It creates a FileOutputStream object and wraps it in an OutputStreamWriter using the default encoding.

Parameters: file name of the file to write to

Throws: FileNotFoundException if the file cannot be written or created

Since: 1.5

PrintWriter

public PrintWriter(String file, String enc)
This initializes a new PrintWriter object to write to the specified file. It creates a FileOutputStream object and wraps it in an OutputStreamWriter using the specified encoding.

Parameters: file name of the file to write to enc the encoding to use

Throws: FileNotFoundException if the file cannot be written or created UnsupportedEncodingException if the encoding is not supported

Since: 1.5

PrintWriter

public PrintWriter(File file)
This initializes a new PrintWriter object to write to the specified file. It creates a FileOutputStream object and wraps it in an OutputStreamWriter using the default encoding.

Parameters: file the file to write to

Throws: FileNotFoundException if the file cannot be written or created

Since: 1.5

PrintWriter

public PrintWriter(File file, String enc)
This initializes a new PrintWriter object to write to the specified file. It creates a FileOutputStream object and wraps it in an OutputStreamWriter using the specified encoding.

Parameters: file the file to write to enc the encoding to use

Throws: FileNotFoundException if the file cannot be written or created UnsupportedEncodingException if the encoding is not supported

Since: 1.5

Method Detail

append

public PrintWriter append(char c)

Since: 1.5

append

public PrintWriter append(CharSequence cs)

Since: 1.5

append

public PrintWriter append(CharSequence cs, int start, int end)

Since: 1.5

checkError

public boolean checkError()
This method checks to see if an error has occurred on this stream. Note that once an error has occurred, this method will continue to report true forever for this stream. Before checking for an error condition, this method flushes the stream.

Returns: true if an error has occurred, false otherwise

close

public void close()
This method closes this stream and all underlying streams.

flush

public void flush()
This method flushes any buffered chars to the underlying stream and then flushes that stream as well.

format

public PrintWriter format(String format, Object... args)

Since: 1.5

format

public PrintWriter format(Locale locale, String format, Object... args)

Since: 1.5

print

public void print(String str)
This method prints a String to the stream. The actual value printed depends on the system default encoding.

Parameters: str The String to print.

print

public void print(char ch)
This method prints a char to the stream. The actual value printed is determined by the character encoding in use.

Parameters: ch The char value to be printed

print

public void print(char[] charArray)
This method prints an array of characters to the stream. The actual value printed depends on the system default encoding.

Parameters: charArray The array of characters to print.

print

public void print(boolean bool)
This methods prints a boolean value to the stream. true values are printed as "true" and false values are printed as "false".

Parameters: bool The boolean value to print

print

public void print(int inum)
This method prints an integer to the stream. The value printed is determined using the String.valueOf() method.

Parameters: inum The int value to be printed

print

public void print(long lnum)
This method prints a long to the stream. The value printed is determined using the String.valueOf() method.

Parameters: lnum The long value to be printed

print

public void print(float fnum)
This method prints a float to the stream. The value printed is determined using the String.valueOf() method.

Parameters: fnum The float value to be printed

print

public void print(double dnum)
This method prints a double to the stream. The value printed is determined using the String.valueOf() method.

Parameters: dnum The double value to be printed

print

public void print(Object obj)
This method prints an Object to the stream. The actual value printed is determined by calling the String.valueOf() method.

Parameters: obj The Object to print.

printf

public PrintWriter printf(String format, Object... args)

Since: 1.5

printf

public PrintWriter printf(Locale locale, String format, Object... args)

Since: 1.5

println

public void println()
This method prints a line separator sequence to the stream. The value printed is determined by the system property line.separator and is not necessarily the Unix '\n' newline character.

println

public void println(boolean bool)
This methods prints a boolean value to the stream. true values are printed as "true" and false values are printed as "false". This method prints a line termination sequence after printing the value.

Parameters: bool The boolean value to print

println

public void println(int inum)
This method prints an integer to the stream. The value printed is determined using the String.valueOf() method. This method prints a line termination sequence after printing the value.

Parameters: inum The int value to be printed

println

public void println(long lnum)
This method prints a long to the stream. The value printed is determined using the String.valueOf() method. This method prints a line termination sequence after printing the value.

Parameters: lnum The long value to be printed

println

public void println(float fnum)
This method prints a float to the stream. The value printed is determined using the String.valueOf() method. This method prints a line termination sequence after printing the value.

Parameters: fnum The float value to be printed

println

public void println(double dnum)
This method prints a double to the stream. The value printed is determined using the String.valueOf() method. This method prints a line termination sequence after printing the value.

Parameters: dnum The double value to be printed

println

public void println(Object obj)
This method prints an Object to the stream. The actual value printed is determined by calling the String.valueOf() method. This method prints a line termination sequence after printing the value.

Parameters: obj The Object to print.

println

public void println(String str)
This method prints a String to the stream. The actual value printed depends on the system default encoding. This method prints a line termination sequence after printing the value.

Parameters: str The String to print.

println

public void println(char ch)
This method prints a char to the stream. The actual value printed is determined by the character encoding in use. This method prints a line termination sequence after printing the value.

Parameters: ch The char value to be printed

println

public void println(char[] charArray)
This method prints an array of characters to the stream. The actual value printed depends on the system default encoding. This method prints a line termination sequence after printing the value.

Parameters: charArray The array of characters to print.

setError

protected void setError()
This method can be called by subclasses to indicate that an error has occurred and should be reported by checkError.

write

public void write(int ch)
This method writes a single char to the stream.

Parameters: ch The char to be written, passed as a int

write

public void write(char[] charArray, int offset, int count)
This method writes count chars from the specified array starting at index offset into the array.

Parameters: charArray The array of chars to write offset The index into the array to start writing from count The number of chars to write

write

public void write(String str, int offset, int count)
This method writes count chars from the specified String to the output starting at character position offset into the String

Parameters: str The String to write chars from offset The offset into the String to start writing from count The number of chars to write.

write

public void write(char[] charArray)
This method write all the chars in the specified array to the output.

Parameters: charArray The array of characters to write

write

public void write(String str)
This method writes the contents of the specified String to the underlying stream.

Parameters: str The String to write