Skip to content

Most visited

Recently visited

navigation
Added in API level 1

PrintWriter

public class PrintWriter
extends Writer

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


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.

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.

Inherited methods

From class java.io.Writer
From class java.lang.Object
From interface java.lang.Appendable
From interface java.io.Closeable
From interface java.io.Flushable
From interface java.lang.AutoCloseable

Fields

out

Added in API level 1
Writer out

The writer to print data to.

Public constructors

PrintWriter

Added in API level 1
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 OutputStream: the target output stream.
Throws
NullPointerException if out is null.

PrintWriter

Added in API level 1
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 OutputStream: the target output stream.
autoFlush boolean: indicates whether contents are flushed upon encountering a newline sequence.
Throws
NullPointerException if out is null.

PrintWriter

Added in API level 1
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 Writer: the target writer.
Throws
NullPointerException if wr is null.

PrintWriter

Added in API level 1
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 Writer: the target writer.
autoFlush boolean: indicates whether to flush contents upon encountering a newline sequence.
Throws
NullPointerException if out is null.

PrintWriter

Added in API level 1
PrintWriter (File file)

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 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.

PrintWriter

Added in API level 1
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 File: the target file. If the file already exists, its contents are removed, otherwise a new file is created.
csn String: 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.

PrintWriter

Added in API level 1
PrintWriter (String fileName)

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 String: 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.

PrintWriter

Added in API level 1
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 String: the target file's name. If the file already exists, its contents are removed, otherwise a new file is created.
csn String: 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

append

Added in API level 1
PrintWriter append (char c)

Appends the character c to the target.

Parameters
c char: the character to append to the target.
Returns
PrintWriter this writer.

append

Added in API level 1
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 CharSequence: the character sequence appended to the target.
start int: the index of the first char in the character sequence appended to the target.
end int: the index of the character following the last character of the subsequence appended to the target.
Returns
PrintWriter 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.

append

Added in API level 1
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 CharSequence: the character sequence appended to the target.
Returns
PrintWriter this writer.

checkError

Added in API level 1
boolean checkError ()

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

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

See also:

close

Added in API level 1
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.

flush

Added in API level 1
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.

format

Added in API level 1
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 Locale: the locale used in the method. No localization will be applied if l is null.
format String: the format string (see format(String, Object...))
args Object: the list of arguments passed to the formatter. If there are more arguments than required by format, additional arguments are ignored.
Returns
PrintWriter 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

format

Added in API level 1
PrintWriter format (String format, 
                Object... args)

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

print

Added in API level 1
void print (float fnum)

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

Parameters
fnum float: the float value to print to the target.

See also:

print

Added in API level 1
void print (double dnum)

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

Parameters
dnum double: the double value to print to the target.

See also:

print

Added in API level 1
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 String: the string to print to the target.

See also:

print

Added in API level 1
void print (Object obj)

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

Parameters
obj Object: the object to print to the target.

See also:

print

Added in API level 1
void print (char ch)

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

Parameters
ch char: the character to print to the target.

See also:

print

Added in API level 1
void print (char[] charArray)

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

Parameters
charArray char: the character array to print to the target.

See also:

print

Added in API level 1
void print (long lnum)

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

Parameters
lnum long: the long value to print to the target.

See also:

print

Added in API level 1
void print (int inum)

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

Parameters
inum int: the integer value to print to the target.

See also:

print

Added in API level 1
void print (boolean bool)

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

Parameters
bool boolean: the boolean value to print the target.

See also:

printf

Added in API level 1
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 Locale: the locale used in the method. No localization will be applied if l is null.
format String: the format string (see format(String, Object...))
args Object: the list of arguments passed to the formatter. If there are more arguments than required by format, additional arguments are ignored.
Returns
PrintWriter 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

printf

Added in API level 1
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.

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

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

println

Added in API level 1
void println ()

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

println

Added in API level 1
void println (float f)

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

Parameters
f float

println

Added in API level 1
void println (int i)

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

Parameters
i int

println

Added in API level 1
void println (long l)

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

Parameters
l long

println

Added in API level 1
void println (Object obj)

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.

Parameters
obj Object

println

Added in API level 1
void println (char[] chars)

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

Parameters
chars char

println

Added in API level 1
void println (String str)

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.

Parameters
str String

println

Added in API level 1
void println (char c)

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

Parameters
c char

println

Added in API level 1
void println (double d)

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

Parameters
d double

println

Added in API level 1
void println (boolean b)

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

Parameters
b boolean

write

Added in API level 1
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 char: the buffer to write to the target.
offset int: the index of the first character in buffer to write.
count int: 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.

write

Added in API level 1
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 int: the character to write to the target.

write

Added in API level 1
void write (char[] buf)

Writes the character buffer buf to the target.

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

write

Added in API level 1
void write (String str, 
                int offset, 
                int count)

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

Parameters
str String: the non-null string containing the characters to write.
offset int: the index of the first character in str to write.
count int: 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.

write

Added in API level 1
void write (String str)

Writes the characters from the specified string to the target.

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

Protected methods

clearError

Added in API level 9
void clearError ()

Sets the error state of the stream to false.

setError

Added in API level 1
void setError ()

Sets the error flag of this writer to true.

This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.