public class PrintWriter extends Writer
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.Modifier and Type | Field and Description |
---|---|
protected Writer |
out
The writer to print data to.
|
Constructor and Description |
---|
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(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(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. |
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. |
Modifier and Type | Method and Description |
---|---|
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.
|
protected void |
clearError()
Sets the error state of the stream to false.
|
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(boolean bool)
Prints the string representation of the specified boolean 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(double dnum)
Prints the string representation of the specified double to the target.
|
void |
print(float fnum)
Prints the string representation of the specified float to the target.
|
void |
print(int inum)
Prints the string representation of the specified integer to the target.
|
void |
print(long lnum)
Prints the string representation of the specified long 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.
|
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(boolean b)
Prints the string representation of the boolean
b followed by a newline. |
void |
println(char c)
Prints the string representation of the char
c followed by a newline. |
void |
println(char[] chars)
Prints the string representation of the character array
chars followed by a newline. |
void |
println(double d)
Prints the string representation of the double
d followed by 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(String str)
Prints the string representation of the string
s followed by a newline. |
protected void |
setError()
Sets the error flag of this writer to true.
|
void |
write(char[] buf)
Writes the character buffer
buf to the target. |
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(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. |
protected Writer out
public PrintWriter(OutputStream out)
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
.public PrintWriter(OutputStream out, boolean autoFlush)
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
.public PrintWriter(Writer wr)
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
.public PrintWriter(Writer wr, boolean autoFlush)
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
.public PrintWriter(File file) throws FileNotFoundException
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.public PrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException
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.public PrintWriter(String fileName) throws FileNotFoundException
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.public PrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException
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.public boolean checkError()
true
if either an IOException
has been thrown
previously or if setError()
has been called;
false
otherwise.setError()
protected void clearError()
public void close()
true
.public void flush()
true
.public PrintWriter format(String format, Object... args)
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 Formatter.format(java.lang.String, java.lang.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
public PrintWriter format(Locale l, String format, Object... args)
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 Formatter.format(java.lang.String, java.lang.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
public PrintWriter printf(String format, Object... args)
#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 Formatter.format(java.lang.String, java.lang.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
public PrintWriter printf(Locale l, String format, Object... args)
#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 Formatter.format(java.lang.String, java.lang.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
public void print(char[] charArray)
charArray
- the character array to print to the target.print(String)
public void print(char ch)
ch
- the character to print to the target.print(String)
public void print(double dnum)
dnum
- the double value to print to the target.print(String)
public void print(float fnum)
fnum
- the float value to print to the target.print(String)
public void print(int inum)
inum
- the integer value to print to the target.print(String)
public void print(long lnum)
lnum
- the long value to print to the target.print(String)
public void print(Object obj)
obj
- the object to print to the target.print(String)
public void print(String str)
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.write(int)
public void print(boolean bool)
bool
- the boolean value to print the target.print(String)
public void println()
true
.public void println(char[] chars)
chars
followed by a newline.
Flushes this writer if the autoFlush flag is set to true
.public void println(char c)
c
followed by a newline.
Flushes this writer if the autoFlush flag is set to true
.public void println(double d)
d
followed by a newline.
Flushes this writer if the autoFlush flag is set to true
.public void println(float f)
f
followed by a newline.
Flushes this writer if the autoFlush flag is set to true
.public void println(int i)
i
followed by a newline.
Flushes this writer if the autoFlush flag is set to true
.public void println(long l)
l
followed by a newline.
Flushes this writer if the autoFlush flag is set to true
.public void println(Object obj)
o
, or "null
,
followed by a newline.
Flushes this writer if the autoFlush flag is set to true
.public void println(String str)
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(boolean b)
b
followed by a newline.
Flushes this writer if the autoFlush flag is set to true
.protected void setError()
public void write(char[] buf)
buf
to the target.public void write(char[] buf, int offset, int count)
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.
write
in class Writer
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
.public void write(int oneChar)
oneChar
are written.
This writer's error flag is set to true
if this writer is closed
or an I/O error occurs.
public void write(String str)
public void write(String str, int offset, int count)
count
characters from str
starting at offset
to the target.write
in class Writer
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
.public PrintWriter append(char c)
c
to the target.append
in interface Appendable
append
in class Writer
c
- the character to append to the target.public PrintWriter append(CharSequence csq)
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.append
in interface Appendable
append
in class Writer
csq
- the character sequence appended to the target.public PrintWriter append(CharSequence csq, int start, int end)
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.append
in interface Appendable
append
in class Writer
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
.