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.
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
out | The writer to print data to. |
[Expand]
Inherited Fields
|
|||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.io.Writer
|
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
Constructs a new
PrintWriter
with
out
as its target
stream.
|
||||||||||
|
Constructs a new
PrintWriter
with
out
as its target
stream.
|
||||||||||
|
Constructs a new
PrintWriter
with
wr
as its target
writer.
|
||||||||||
|
Constructs a new
PrintWriter
with
out
as its target
writer.
|
||||||||||
|
Constructs a new
PrintWriter
with
file
as its target.
|
||||||||||
|
Constructs a new
PrintWriter
with
file
as its target.
|
||||||||||
|
Constructs a new
PrintWriter
with the file identified by
fileName
as its target.
|
||||||||||
|
Constructs a new
PrintWriter
with the file identified by
fileName
as its target.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
Appends the character
c
to the target.
|
||||||||||
|
Appends a subsequence of the character sequence
csq
to the
target.
|
||||||||||
|
Appends the character sequence
csq
to the target.
|
||||||||||
|
Flushes this writer and returns the value of the error flag.
|
||||||||||
|
Closes this print writer.
|
||||||||||
|
Ensures that all pending data is sent out to the target.
|
||||||||||
|
Writes a string formatted by an intermediate
Formatter
to the
target using the specified locale, format string and arguments.
|
||||||||||
|
Formats
args
according to the format string
format
, and writes the result
to this stream.
|
||||||||||
|
Prints the string representation of the specified float to the target.
|
||||||||||
|
Prints the string representation of the specified double to the target.
|
||||||||||
|
Prints a string to the target.
|
||||||||||
|
Prints the string representation of the specified object to the target.
|
||||||||||
|
Prints the string representation of the specified character to the
target.
|
||||||||||
|
Prints the string representation of the specified character array
to the target.
|
||||||||||
|
Prints the string representation of the specified long to the target.
|
||||||||||
|
Prints the string representation of the specified integer to the target.
|
||||||||||
|
Prints the string representation of the specified boolean to the target.
|
||||||||||
|
Prints a formatted string.
|
||||||||||
|
Prints a formatted string.
|
||||||||||
|
Prints a newline.
|
||||||||||
|
Prints the string representation of the float
f
followed by a newline.
|
||||||||||
|
Prints the string representation of the int
i
followed by a newline.
|
||||||||||
|
Prints the string representation of the long
l
followed by a newline.
|
||||||||||
|
Prints the string representation of the object
o
, or
"null
,
followed by a newline.
|
||||||||||
|
Prints the string representation of the character array
chars
followed by a newline.
|
||||||||||
|
Prints the string representation of the string
s
followed by a newline.
|
||||||||||
|
Prints the string representation of the char
c
followed by a newline.
|
||||||||||
|
Prints the string representation of the double
d
followed by a newline.
|
||||||||||
|
Prints the string representation of the boolean
b
followed by a newline.
|
||||||||||
|
Writes
count
characters from
buffer
starting at
offset
to the target.
|
||||||||||
|
Writes one character to the target.
|
||||||||||
|
Writes the character buffer
buf
to the target.
|
||||||||||
|
Writes
count
characters from
str
starting at
offset
to the target.
|
||||||||||
|
Writes the characters from the specified string to the target.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
Sets the error state of the stream to false.
|
||||||||||
|
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
|
|||||||||||
From interface
java.lang.AutoCloseable
|
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.
out | the target output stream. |
---|
NullPointerException |
if
out
is
null
.
|
---|
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.
out | the target output stream. |
---|---|
autoFlush | indicates whether contents are flushed upon encountering a newline sequence. |
NullPointerException |
if
out
is
null
.
|
---|
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.
wr | the target writer. |
---|
NullPointerException |
if
wr
is
null
.
|
---|
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.
wr | the target writer. |
---|---|
autoFlush | indicates whether to flush contents upon encountering a newline sequence. |
NullPointerException |
if
out
is
null
.
|
---|
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.
file | the target file. If the file already exists, its contents are removed, otherwise a new file is created. |
---|
FileNotFoundException | if an error occurs while opening or creating the target file. |
---|
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.
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. |
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.
|
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.
fileName | the target file's name. If the file already exists, its contents are removed, otherwise a new file is created. |
---|
FileNotFoundException | if an error occurs while opening or creating the target file. |
---|
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.
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. |
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.
|
Appends the character
c
to the target.
c | the character to append to the target. |
---|
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.
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. |
StringIndexOutOfBoundsException |
if
start > end
,
start < 0
,
end < 0
or
either
start
or
end
are greater or equal than
the length of
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.
csq | the character sequence appended to the target. |
---|
Flushes this writer and returns the value of the error flag.
true
if either an
IOException
has been thrown
previously or if
setError()
has been called;
false
otherwise.
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
.
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
.
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.
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.
|
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
|
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.
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.
|
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
|
Prints the string representation of the specified float to the target.
fnum | the float value to print to the target. |
---|
Prints the string representation of the specified double to the target.
dnum | the double value to print to the target. |
---|
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
.
str | the string to print to the target. |
---|
Prints the string representation of the specified object to the target.
obj | the object to print to the target. |
---|
Prints the string representation of the specified character to the target.
ch | the character to print to the target. |
---|
Prints the string representation of the specified character array to the target.
charArray | the character array to print to the target. |
---|
Prints the string representation of the specified long to the target.
lnum | the long value to print to the target. |
---|
Prints the string representation of the specified integer to the target.
inum | the integer value to print to the target. |
---|
Prints the string representation of the specified boolean to the target.
bool | the boolean value to print the target. |
---|
Prints a formatted string. The behavior of this method is the same as
this writer's
#format(Locale, String, Object...)
method.
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.
|
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
|
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
".
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.
|
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
|
Prints a newline. Flushes this writer if the autoFlush flag is set to
true
.
Prints the string representation of the float
f
followed by a newline.
Flushes this writer if the autoFlush flag is set to
true
.
Prints the string representation of the int
i
followed by a newline.
Flushes this writer if the autoFlush flag is set to
true
.
Prints the string representation of the long
l
followed by a newline.
Flushes this writer if the autoFlush flag is set to
true
.
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
.
Prints the string representation of the character array
chars
followed by a newline.
Flushes this writer if the autoFlush flag is set to
true
.
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
.
Prints the string representation of the char
c
followed by a newline.
Flushes this writer if the autoFlush flag is set to
true
.
Prints the string representation of the double
d
followed by a newline.
Flushes this writer if the autoFlush flag is set to
true
.
Prints the string representation of the boolean
b
followed by a newline.
Flushes this writer if the autoFlush flag is set to
true
.
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.
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.
|
IndexOutOfBoundsException |
if
offset < 0
or
count < 0
, or if
offset + count
is greater than the length of
buf
.
|
---|
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.
oneChar | the character to write to the target. |
---|
Writes the character buffer
buf
to the target.
buf | the non-null array containing characters to write. |
---|
Writes
count
characters from
str
starting at
offset
to the target.
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.
|
IndexOutOfBoundsException |
if
offset < 0
or
count < 0
, or if
offset + count
is greater than the length of
str
.
|
---|
Writes the characters from the specified string to the target.
str | the non-null string containing the characters to write. |
---|