public class

PrintWriter

extends Writer
java.lang.Object
   ↳ java.io.Writer
     ↳ java.io.PrintWriter

Class Overview

Wraps either an existing OutputStream or an existing Writer and provides convenience methods for printing common data types in a human readable format. No IOException is thrown by this class. Instead, callers should use checkError() to see if a problem has occurred in this writer.

Summary

Fields
protected Writer out The writer to print data to.
[Expand]
Inherited Fields
From class java.io.Writer
Public Constructors
PrintWriter(OutputStream out)
Constructs a new PrintWriter with out as its target stream.
PrintWriter(OutputStream out, boolean autoflush)
Constructs a new PrintWriter with out as its target stream.
PrintWriter(Writer wr)
Constructs a new PrintWriter with wr as its target writer.
PrintWriter(Writer wr, boolean autoflush)
Constructs a new PrintWriter with out as its target writer.
PrintWriter(File file)
Constructs a new PrintWriter with file as its target.
PrintWriter(File file, String csn)
Constructs a new PrintWriter with file as its target.
PrintWriter(String fileName)
Constructs a new PrintWriter with the file identified by fileName as its target.
PrintWriter(String fileName, String csn)
Constructs a new PrintWriter with the file identified by fileName as its target.
Public Methods
PrintWriter append(char c)
Appends the character c to the target.
PrintWriter append(CharSequence csq)
Appends the character sequence csq to the target.
PrintWriter append(CharSequence csq, int start, int end)
Appends a subsequence of the character sequence csq to the target.
boolean checkError()
Flushes this writer and returns the value of the error flag.
void close()
Closes this print writer.
void flush()
Ensures that all pending data is sent out to the target.
PrintWriter format(String format, Object... args)
Writes a string formatted by an intermediate Formatter to the target using the specified format string and arguments.
PrintWriter format(Locale l, String format, Object... args)
Writes a string formatted by an intermediate Formatter to the target using the specified locale, format string and arguments.
void print(float fnum)
Prints the string representation of the specified float to the target.
void print(char ch)
Prints the string representation of the specified character to the target.
void print(Object obj)
Prints the string representation of the specified object to the target.
void print(String str)
Prints a string to the target.
void print(int inum)
Prints the string representation of the specified integer to the target.
void print(double dnum)
Prints the string representation of the specified double to the target.
void print(long lnum)
Prints the string representation of the specified long to the target.
void print(char[] charArray)
Prints the string representation of the specified character array to the target.
void print(boolean bool)
Prints the string representation of the specified boolean to the target.
PrintWriter printf(Locale l, String format, Object... args)
Prints a formatted string.
PrintWriter printf(String format, Object... args)
Prints a formatted string.
void println(double dnum)
Prints the string representation of the specified double followed by the system property "line.separator" to the target.
void println(String str)
Prints a string followed by the system property "line.separator" to the target.
void println(boolean bool)
Prints the string representation of the specified boolean followed by the system property "line.separator" to the target.
void println(long lnum)
Prints the string representation of the specified long followed by the system property "line.separator" to the target.
void println(char ch)
Prints the string representation of the specified character followed by the system property "line.separator" to the target.
void println(float fnum)
Prints the string representation of the specified float followed by the system property "line.separator" to the target.
void println(char[] charArray)
Prints the string representation of the specified character array followed by the system property "line.separator" to the target.
void println(Object obj)
Prints the string representation of the specified object followed by the system property "line.separator" to the target.
void println()
Prints the string representation of the system property "line.separator" to the target.
void println(int inum)
Prints the string representation of the specified integer followed by the system property "line.separator" to the target.
void write(char[] buf)
Writes the character buffer buf to the target.
void write(String str)
Writes the characters from the specified string to the target.
void write(String str, int offset, int count)
Writes count characters from str starting at offset to the target.
void write(int oneChar)
Writes one character to the target.
void write(char[] buf, int offset, int count)
Writes count characters from buffer starting at offset to the target.
Protected Methods
void setError()
Sets the error flag of this writer to true.
[Expand]
Inherited Methods
From class java.io.Writer
From class java.lang.Object
From interface java.io.Closeable
From interface java.io.Flushable
From interface java.lang.Appendable

Fields

protected Writer out

The writer to print data to.

Public Constructors

public PrintWriter (OutputStream out)

Constructs a new PrintWriter with out as its target stream. By default, the new print writer does not automatically flush its contents to the target stream when a newline is encountered.

Parameters
out the target output stream.
Throws
NullPointerException if out is null.

public PrintWriter (OutputStream out, boolean autoflush)

Constructs a new PrintWriter with out as its target stream. The parameter autoflush determines if the print writer automatically flushes its contents to the target stream when a newline is encountered.

Parameters
out the target output stream.
autoflush indicates whether contents are flushed upon encountering a newline sequence.
Throws
NullPointerException if out is null.

public PrintWriter (Writer wr)

Constructs a new PrintWriter with wr as its target writer. By default, the new print writer does not automatically flush its contents to the target writer when a newline is encountered.

Parameters
wr the target writer.
Throws
NullPointerException if wr is null.

public PrintWriter (Writer wr, boolean autoflush)

Constructs a new PrintWriter with out as its target writer. The parameter autoflush determines if the print writer automatically flushes its contents to the target writer when a newline is encountered.

Parameters
wr the target writer.
autoflush indicates whether to flush contents upon encountering a newline sequence.
Throws
NullPointerException if out is null.

public PrintWriter (File file)

Constructs a new PrintWriter with file as its target. The virtual machine's default character set is used for character encoding. The print writer does not automatically flush its contents to the target file when a newline is encountered. The output to the file is buffered.

Parameters
file the target file. If the file already exists, its contents are removed, otherwise a new file is created.
Throws
FileNotFoundException if an error occurs while opening or creating the target file.
SecurityException if a security manager exists and it denies writing to the target file.

public PrintWriter (File file, String csn)

Constructs a new PrintWriter with file as its target. The character set named csn is used for character encoding. The print writer does not automatically flush its contents to the target file when a newline is encountered. The output to the file is buffered.

Parameters
file the target file. If the file already exists, its contents are removed, otherwise a new file is created.
csn the name of the character set used for character encoding.
Throws
FileNotFoundException if an error occurs while opening or creating the target file.
NullPointerException if csn is null.
SecurityException if a security manager exists and it denies writing to the target file.
UnsupportedEncodingException if the encoding specified by csn is not supported.

public PrintWriter (String fileName)

Constructs a new PrintWriter with the file identified by fileName as its target. The virtual machine's default character set is used for character encoding. The print writer does not automatically flush its contents to the target file when a newline is encountered. The output to the file is buffered.

Parameters
fileName the target file's name. If the file already exists, its contents are removed, otherwise a new file is created.
Throws
FileNotFoundException if an error occurs while opening or creating the target file.
SecurityException if a security manager exists and it denies writing to the target file.

public PrintWriter (String fileName, String csn)

Constructs a new PrintWriter with the file identified by fileName as its target. The character set named csn is used for character encoding. The print writer does not automatically flush its contents to the target file when a newline is encountered. The output to the file is buffered.

Parameters
fileName the target file's name. If the file already exists, its contents are removed, otherwise a new file is created.
csn the name of the character set used for character encoding.
Throws
FileNotFoundException if an error occurs while opening or creating the target file.
NullPointerException if csn is null.
SecurityException if a security manager exists and it denies writing to the target file.
UnsupportedEncodingException if the encoding specified by csn is not supported.

Public Methods

public PrintWriter append (char c)

Appends the character c to the target.

Parameters
c the character to append to the target.
Returns
  • this writer.

public PrintWriter append (CharSequence csq)

Appends the character sequence csq to the target. This method works the same way as PrintWriter.print(csq.toString()). If csq is null, then the string "null" is written to the target.

Parameters
csq the character sequence appended to the target.
Returns
  • this writer.

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

Appends a subsequence of the character sequence csq to the target. This method works the same way as PrintWriter.print(csq.subsequence(start, end).toString()). If csq is null, then the specified subsequence of the string "null" will be written to the target.

Parameters
csq the character sequence appended to the target.
start the index of the first char in the character sequence appended to the target.
end the index of the character following the last character of the subsequence appended to the target.
Returns
  • this writer.
Throws
StringIndexOutOfBoundsException if start > end, start < 0, end < 0 or either start or end are greater or equal than the length of csq.

public boolean checkError ()

Flushes this writer and returns the value of the error flag.

Returns
  • true if either an IOException has been thrown previously or if setError() has been called; false otherwise.
See Also

public void close ()

Closes this print writer. Flushes this writer and then closes the target. If an I/O error occurs, this writer's error flag is set to true.

public void flush ()

Ensures that all pending data is sent out to the target. It also flushes the target. If an I/O error occurs, this writer's error state is set to true.

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

Writes a string formatted by an intermediate Formatter to the target using the specified format string and arguments. For the locale, the default value of the current virtual machine instance is used. If automatic flushing is enabled then the buffer is flushed as well.

Parameters
format the format string used for format(String, Object...).
args the list of arguments passed to the formatter. If there are more arguments than required by the format string, then the additional arguments are ignored.
Returns
  • this writer.
Throws
IllegalFormatException if the format string is illegal or incompatible with the arguments, if there are not enough arguments or if any other error regarding the format string or arguments is detected.
NullPointerException if format is null.

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

Writes a string formatted by an intermediate Formatter to the target using the specified locale, format string and arguments. If automatic flushing is enabled then this writer is flushed.

Parameters
l the locale used in the method. No localization will be applied if l is null.
format the format string used for format(String, Object...).
args the list of arguments passed to the formatter. If there are more arguments than required by the format string, then the additional arguments are ignored.
Returns
  • this writer.
Throws
IllegalFormatException if the format string is illegal or incompatible with the arguments, if there are not enough arguments or if any other error regarding the format string or arguments is detected.
NullPointerException if format is null.

public void print (float fnum)

Prints the string representation of the specified float to the target.

Parameters
fnum the float value to print to the target.
See Also

public void print (char ch)

Prints the string representation of the specified character to the target.

Parameters
ch the character to print to the target.
See Also

public void print (Object obj)

Prints the string representation of the specified object to the target.

Parameters
obj the object to print to the target.
See Also

public void print (String str)

Prints a string to the target. The string is converted to an array of bytes using the encoding chosen during the construction of this writer. The bytes are then written to the target with write(int).

If an I/O error occurs, this writer's error flag is set to true.

Parameters
str the string to print to the target.
See Also

public void print (int inum)

Prints the string representation of the specified integer to the target.

Parameters
inum the integer value to print to the target.
See Also

public void print (double dnum)

Prints the string representation of the specified double to the target.

Parameters
dnum the double value to print to the target.
See Also

public void print (long lnum)

Prints the string representation of the specified long to the target.

Parameters
lnum the long value to print to the target.
See Also

public void print (char[] charArray)

Prints the string representation of the specified character array to the target.

Parameters
charArray the character array to print to the target.
See Also

public void print (boolean bool)

Prints the string representation of the specified boolean to the target.

Parameters
bool the boolean value to print the target.
See Also

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

Prints a formatted string. The behavior of this method is the same as this writer's #format(Locale, String, Object...) method.

Parameters
l the locale used in the method. No localization will be applied if l is null.
format the format string used for format(String, Object...).
args the list of arguments passed to the formatter. If there are more arguments than required by the format string, then the additional arguments are ignored.
Returns
  • this writer.
Throws
IllegalFormatException if the format string is illegal or incompatible with the arguments, if there are not enough arguments or if any other error regarding the format string or arguments is detected.
NullPointerException if format is null.

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

Prints a formatted string. The behavior of this method is the same as this writer's #format(String, Object...) method. For the locale, the default value of the current virtual machine instance is used.

Parameters
format the format string used for format(String, Object...).
args the list of arguments passed to the formatter. If there are more arguments than required by the format string, then the additional arguments are ignored.
Returns
  • this writer.
Throws
IllegalFormatException if the format string is illegal or incompatible with the arguments, if there are not enough arguments or if any other error regarding the format string or arguments is detected.
NullPointerException if format is null.

public void println (double dnum)

Prints the string representation of the specified double followed by the system property "line.separator" to the target. Flushes this writer if the autoflush flag is set to true.

Parameters
dnum the double value to print to the target.
See Also

public void println (String str)

Prints a string followed by the system property "line.separator" to the target. The string is converted to an array of bytes using the encoding chosen during the construction of this writer. The bytes are then written to the target with write(int). Finally, this writer is flushed if the autoflush flag is set to true.

If an I/O error occurs, this writer's error flag is set to true.

Parameters
str the string to print to the target.
See Also

public void println (boolean bool)

Prints the string representation of the specified boolean followed by the system property "line.separator" to the target. Flushes this writer if the autoflush flag is set to true.

Parameters
bool the boolean value to print to the target.
See Also

public void println (long lnum)

Prints the string representation of the specified long followed by the system property "line.separator" to the target. Flushes this writer if the autoflush flag is set to true.

Parameters
lnum the long value to print to the target.
See Also

public void println (char ch)

Prints the string representation of the specified character followed by the system property "line.separator" to the target. Flushes this writer if the autoflush flag is set to true.

Parameters
ch the character to print to the target.
See Also

public void println (float fnum)

Prints the string representation of the specified float followed by the system property "line.separator" to the target. Flushes this writer if the autoflush flag is set to true.

Parameters
fnum the float value to print to the target.
See Also

public void println (char[] charArray)

Prints the string representation of the specified character array followed by the system property "line.separator" to the target. Flushes this writer if the autoflush flag is set to true.

Parameters
charArray the character array to print to the target.
See Also

public void println (Object obj)

Prints the string representation of the specified object followed by the system property "line.separator" to the target. Flushes this writer if the autoflush flag is set to true.

Parameters
obj the object to print to the target.
See Also

public void println ()

Prints the string representation of the system property "line.separator" to the target. Flushes this writer if the autoflush flag is set to true.

public void println (int inum)

Prints the string representation of the specified integer followed by the system property "line.separator" to the target. Flushes this writer if the autoflush flag is set to true.

Parameters
inum the integer value to print to the target.
See Also

public void write (char[] buf)

Writes the character buffer buf to the target.

Parameters
buf the non-null array containing characters to write.

public void write (String str)

Writes the characters from the specified string to the target.

Parameters
str the non-null string containing the characters to write.

public void write (String str, int offset, int count)

Writes count characters from str starting at offset to the target.

Parameters
str the non-null string containing the characters to write.
offset the index of the first character in str to write.
count the number of characters from str to write.
Throws
IndexOutOfBoundsException if offset < 0 or count < 0, or if offset + count is greater than the length of str.

public void write (int oneChar)

Writes one character to the target. Only the two least significant bytes of the integer oneChar are written.

This writer's error flag is set to true if this writer is closed or an I/O error occurs.

Parameters
oneChar the character to write to the target.

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

Writes count characters from buffer starting at offset to the target.

This writer's error flag is set to true if this writer is closed or an I/O error occurs.

Parameters
buf the buffer to write to the target.
offset the index of the first character in buffer to write.
count the number of characters in buffer to write.
Throws
IndexOutOfBoundsException if offset < 0 or count < 0, or if offset + count is greater than the length of buf.

Protected Methods

protected void setError ()

Sets the error flag of this writer to true.