to top
Android APIs
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, int start, int end)
Appends a subsequence of the character sequence csq to the target.
PrintWriter append(CharSequence csq)
Appends 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(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.
PrintWriter format(String format, Object... args)
Formats args according to the format string format, and writes the result to this stream.
void print(float fnum)
Prints the string representation of the specified float to the target.
void print(double dnum)
Prints the string representation of the specified double to the target.
void print(String str)
Prints a string to the target.
void print(Object obj)
Prints the string representation of the specified object to the target.
void print(char ch)
Prints the string representation of the specified character to the target.
void print(char[] charArray)
Prints the string representation of the specified character array to the target.
void print(long lnum)
Prints the string representation of the specified long to the target.
void print(int inum)
Prints the string representation of the specified integer 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()
Prints a newline.
void println(float f)
Prints the string representation of the float f followed by a newline.
void println(int i)
Prints the string representation of the int i followed by a newline.
void println(long l)
Prints the string representation of the long l followed by a newline.
void println(Object obj)
Prints the string representation of the object o, or "null, followed by a newline.
void println(char[] chars)
Prints the string representation of the character array chars followed by a newline.
void println(String str)
Prints the string representation of the string s followed by a newline.
void println(char c)
Prints the string representation of the char c followed by a newline.
void println(double d)
Prints the string representation of the double d followed by a newline.
void println(boolean b)
Prints the string representation of the boolean b followed by a newline.
void write(char[] buf, int offset, int count)
Writes count characters from buffer starting at offset to the target.
void write(int oneChar)
Writes one character to the target.
void write(char[] buf)
Writes the character buffer buf to the target.
void write(String str, int offset, int count)
Writes count characters from str starting at offset to the target.
void write(String str)
Writes the characters from the specified string to the target.
Protected Methods
void clearError()
Sets the error state of the stream to false.
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

Since: API Level 1

The writer to print data to.

Public Constructors

public PrintWriter (OutputStream out)

Since: API Level 1

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)

Since: API Level 1

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)

Since: API Level 1

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)

Since: API Level 1

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)

Since: API Level 1

Constructs a new PrintWriter with file as its target. The VM'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.

public PrintWriter (File file, String csn)

Since: API Level 1

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.
UnsupportedEncodingException if the encoding specified by csn is not supported.

public PrintWriter (String fileName)

Since: API Level 1

Constructs a new PrintWriter with the file identified by fileName as its target. The VM'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.

public PrintWriter (String fileName, String csn)

Since: API Level 1

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.
UnsupportedEncodingException if the encoding specified by csn is not supported.

Public Methods

public PrintWriter append (char c)

Since: API Level 1

Appends the character c to the target.

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

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

Since: API Level 1

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 PrintWriter append (CharSequence csq)

Since: API Level 1

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 boolean checkError ()

Since: API Level 1

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 ()

Since: API Level 1

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 ()

Since: API Level 1

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 (Locale l, String format, Object... args)

Since: API Level 1

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 (see format(String, Object...))
args the list of arguments passed to the formatter. If there are more arguments than required by format, 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 == null

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

Since: API Level 1

Formats args according to the format string format, and writes the result to this stream. This method uses the user's default locale. See "Be wary of the default locale". If automatic flushing is enabled then the buffer is flushed as well.

Parameters
format the format string (see format(String, Object...))
args the list of arguments passed to the formatter. If there are more arguments than required by format, 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 == null

public void print (float fnum)

Since: API Level 1

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 (double dnum)

Since: API Level 1

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 (String str)

Since: API Level 1

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 (Object obj)

Since: API Level 1

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 (char ch)

Since: API Level 1

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 (char[] charArray)

Since: API Level 1

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 (long lnum)

Since: API Level 1

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 (int inum)

Since: API Level 1

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 (boolean bool)

Since: API Level 1

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)

Since: API Level 1

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 (see format(String, Object...))
args the list of arguments passed to the formatter. If there are more arguments than required by format, 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 == null

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

Since: API Level 1

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

The Locale used is the user's default locale. See "Be wary of the default locale".

Parameters
format the format string (see format(String, Object...))
args the list of arguments passed to the formatter. If there are more arguments than required by format, 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 == null

public void println ()

Since: API Level 1

Prints a newline. Flushes this writer if the autoFlush flag is set to true.

public void println (float f)

Since: API Level 1

Prints the string representation of the float f followed by a newline. Flushes this writer if the autoFlush flag is set to true.

public void println (int i)

Since: API Level 1

Prints the string representation of the int i followed by a newline. Flushes this writer if the autoFlush flag is set to true.

public void println (long l)

Since: API Level 1

Prints the string representation of the long l followed by a newline. Flushes this writer if the autoFlush flag is set to true.

public void println (Object obj)

Since: API Level 1

Prints the string representation of the object o, or "null, followed by a newline. Flushes this writer if the autoFlush flag is set to true.

public void println (char[] chars)

Since: API Level 1

Prints the string representation of the character array chars followed by a newline. Flushes this writer if the autoFlush flag is set to true.

public void println (String str)

Since: API Level 1

Prints the string representation of the string s followed by a newline. Flushes this writer if the autoFlush flag is set to true.

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.

public void println (char c)

Since: API Level 1

Prints the string representation of the char c followed by a newline. Flushes this writer if the autoFlush flag is set to true.

public void println (double d)

Since: API Level 1

Prints the string representation of the double d followed by a newline. Flushes this writer if the autoFlush flag is set to true.

public void println (boolean b)

Since: API Level 1

Prints the string representation of the boolean b followed by a newline. Flushes this writer if the autoFlush flag is set to true.

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

Since: API Level 1

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.

public void write (int oneChar)

Since: API Level 1

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)

Since: API Level 1

Writes the character buffer buf to the target.

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

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

Since: API Level 1

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 (String str)

Since: API Level 1

Writes the characters from the specified string to the target.

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

Protected Methods

protected void clearError ()

Since: API Level 9

Sets the error state of the stream to false.

protected void setError ()

Since: API Level 1

Sets the error flag of this writer to true.