Package java.io
Class PrintWriter
java.lang.Object
java.io.Writer
java.io.PrintWriter
- All Implemented Interfaces:
Closeable,Flushable,Appendable,AutoCloseable
public class PrintWriter extends Writer
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.-
Field Summary
-
Constructor Summary
Constructors Constructor Description PrintWriter(File file)Constructs a newPrintWriterwithfileas its target.PrintWriter(File file, String csn)Constructs a newPrintWriterwithfileas its target.PrintWriter(OutputStream out)Constructs a newPrintWriterwithoutas its target stream.PrintWriter(OutputStream out, boolean autoFlush)Constructs a newPrintWriterwithoutas its target stream.PrintWriter(Writer wr)Constructs a newPrintWriterwithwras its target writer.PrintWriter(Writer wr, boolean autoFlush)Constructs a newPrintWriterwithoutas its target writer.PrintWriter(String fileName)Constructs a newPrintWriterwith the file identified byfileNameas its target.PrintWriter(String fileName, String csn)Constructs a newPrintWriterwith the file identified byfileNameas its target. -
Method Summary
Modifier and Type Method Description PrintWriterappend(char c)Appends the charactercto the target.PrintWriterappend(CharSequence csq)Appends the character sequencecsqto the target.PrintWriterappend(CharSequence csq, int start, int end)Appends a subsequence of the character sequencecsqto the target.booleancheckError()Flushes this writer and returns the value of the error flag.protected voidclearError()Sets the error state of the stream to false.voidclose()Closes this print writer.voidflush()Ensures that all pending data is sent out to the target.PrintWriterformat(String format, Object... args)Formatsargsaccording to the format stringformat, and writes the result to this stream.PrintWriterformat(Locale l, String format, Object... args)Writes a string formatted by an intermediateFormatterto the target using the specified locale, format string and arguments.voidprint(boolean bool)Prints the string representation of the specified boolean to the target.voidprint(char ch)Prints the string representation of the specified character to the target.voidprint(char[] charArray)Prints the string representation of the specified character array to the target.voidprint(double dnum)Prints the string representation of the specified double to the target.voidprint(float fnum)Prints the string representation of the specified float to the target.voidprint(int inum)Prints the string representation of the specified integer to the target.voidprint(long lnum)Prints the string representation of the specified long to the target.voidprint(Object obj)Prints the string representation of the specified object to the target.voidprint(String str)Prints a string to the target.PrintWriterprintf(String format, Object... args)Prints a formatted string.PrintWriterprintf(Locale l, String format, Object... args)Prints a formatted string.voidprintln()Prints a newline.voidprintln(boolean b)Prints the string representation of the booleanbfollowed by a newline.voidprintln(char c)Prints the string representation of the charcfollowed by a newline.voidprintln(char[] chars)Prints the string representation of the character arraycharsfollowed by a newline.voidprintln(double d)Prints the string representation of the doubledfollowed by a newline.voidprintln(float f)Prints the string representation of the floatffollowed by a newline.voidprintln(int i)Prints the string representation of the intifollowed by a newline.voidprintln(long l)Prints the string representation of the longlfollowed by a newline.voidprintln(Object obj)Prints the string representation of the objecto, or"null, followed by a newline.voidprintln(String str)Prints the string representation of the stringsfollowed by a newline.protected voidsetError()Sets the error flag of this writer to true.voidwrite(char[] buf)Writes the character bufferbufto the target.voidwrite(char[] buf, int offset, int count)Writescountcharacters frombufferstarting atoffsetto the target.voidwrite(int oneChar)Writes one character to the target.voidwrite(String str)Writes the characters from the specified string to the target.voidwrite(String str, int offset, int count)Writescountcharacters fromstrstarting atoffsetto the target.
-
Field Details
-
out
The writer to print data to.
-
-
Constructor Details
-
PrintWriter
Constructs a newPrintWriterwithoutas 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- ifoutisnull.
-
PrintWriter
Constructs a newPrintWriterwithoutas its target stream. The parameterautoFlushdetermines 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- ifoutisnull.
-
PrintWriter
Constructs a newPrintWriterwithwras 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- ifwrisnull.
-
PrintWriter
Constructs a newPrintWriterwithoutas its target writer. The parameterautoFlushdetermines 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- ifoutisnull.
-
PrintWriter
Constructs a newPrintWriterwithfileas 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.
-
PrintWriter
public PrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingExceptionConstructs a newPrintWriterwithfileas its target. The character set namedcsnis 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- ifcsnisnull.UnsupportedEncodingException- if the encoding specified bycsnis not supported.
-
PrintWriter
Constructs a newPrintWriterwith the file identified byfileNameas 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.
-
PrintWriter
public PrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingExceptionConstructs a newPrintWriterwith the file identified byfileNameas its target. The character set namedcsnis 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- ifcsnisnull.UnsupportedEncodingException- if the encoding specified bycsnis not supported.
-
-
Method Details
-
checkError
public boolean checkError()Flushes this writer and returns the value of the error flag.- Returns:
trueif either anIOExceptionhas been thrown previously or ifsetError()has been called;falseotherwise.- See Also:
setError()
-
clearError
protected void clearError()Sets the error state of the stream to false.- Since:
- 1.6
-
close
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 totrue. -
flush
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 totrue. -
format
Formatsargsaccording to the format stringformat, 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 (seeFormatter.format(java.lang.String, java.lang.Object...))args- the list of arguments passed to the formatter. If there are more arguments than required byformat, 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- ifformat == null
-
format
Writes a string formatted by an intermediateFormatterto 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 iflisnull.format- the format string (seeFormatter.format(java.lang.String, java.lang.Object...))args- the list of arguments passed to the formatter. If there are more arguments than required byformat, 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- ifformat == null
-
printf
Prints a formatted string. The behavior of this method is the same as this writer's#format(String, Object...)method.The
Localeused is the user's default locale. See "Be wary of the default locale".- Parameters:
format- the format string (seeFormatter.format(java.lang.String, java.lang.Object...))args- the list of arguments passed to the formatter. If there are more arguments than required byformat, 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- ifformat == null
-
printf
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 iflisnull.format- the format string (seeFormatter.format(java.lang.String, java.lang.Object...))args- the list of arguments passed to the formatter. If there are more arguments than required byformat, 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- ifformat == null
-
print
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:
print(String)
-
print
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:
print(String)
-
print
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:
print(String)
-
print
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:
print(String)
-
print
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:
print(String)
-
print
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:
print(String)
-
print
Prints the string representation of the specified object to the target.- Parameters:
obj- the object to print to the target.- See Also:
print(String)
-
print
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 withwrite(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:
write(int)
-
print
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:
print(String)
-
println
public void println()Prints a newline. Flushes this writer if the autoFlush flag is set totrue. -
println
public void println(char[] chars)Prints the string representation of the character arraycharsfollowed by a newline. Flushes this writer if the autoFlush flag is set totrue. -
println
public void println(char c)Prints the string representation of the charcfollowed by a newline. Flushes this writer if the autoFlush flag is set totrue. -
println
public void println(double d)Prints the string representation of the doubledfollowed by a newline. Flushes this writer if the autoFlush flag is set totrue. -
println
public void println(float f)Prints the string representation of the floatffollowed by a newline. Flushes this writer if the autoFlush flag is set totrue. -
println
public void println(int i)Prints the string representation of the intifollowed by a newline. Flushes this writer if the autoFlush flag is set totrue. -
println
public void println(long l)Prints the string representation of the longlfollowed by a newline. Flushes this writer if the autoFlush flag is set totrue. -
println
Prints the string representation of the objecto, or"null, followed by a newline. Flushes this writer if the autoFlush flag is set totrue. -
println
Prints the string representation of the stringsfollowed by a newline. Flushes this writer if the autoFlush flag is set totrue.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 totrue.If an I/O error occurs, this writer's error flag is set to
true. -
println
public void println(boolean b)Prints the string representation of the booleanbfollowed by a newline. Flushes this writer if the autoFlush flag is set totrue. -
setError
protected void setError()Sets the error flag of this writer to true. -
write
public void write(char[] buf)Writes the character bufferbufto the target. -
write
public void write(char[] buf, int offset, int count)Writescountcharacters frombufferstarting atoffsetto the target.This writer's error flag is set to
trueif this writer is closed or an I/O error occurs.- Specified by:
writein classWriter- Parameters:
buf- the buffer to write to the target.offset- the index of the first character inbufferto write.count- the number of characters inbufferto write.- Throws:
IndexOutOfBoundsException- ifoffset < 0orcount < 0, or ifoffset + countis greater than the length ofbuf.
-
write
public void write(int oneChar)Writes one character to the target. Only the two least significant bytes of the integeroneCharare written.This writer's error flag is set to
trueif this writer is closed or an I/O error occurs. -
write
Writes the characters from the specified string to the target. -
write
Writescountcharacters fromstrstarting atoffsetto the target.- Overrides:
writein classWriter- Parameters:
str- the non-null string containing the characters to write.offset- the index of the first character instrto write.count- the number of characters fromstrto write.- Throws:
IndexOutOfBoundsException- ifoffset < 0orcount < 0, or ifoffset + countis greater than the length ofstr.
-
append
Appends the charactercto the target.- Specified by:
appendin interfaceAppendable- Overrides:
appendin classWriter- Parameters:
c- the character to append to the target.- Returns:
- this writer.
-
append
Appends the character sequencecsqto the target. This method works the same way asPrintWriter.print(csq.toString()). Ifcsqisnull, then the string "null" is written to the target.- Specified by:
appendin interfaceAppendable- Overrides:
appendin classWriter- Parameters:
csq- the character sequence appended to the target.- Returns:
- this writer.
-
append
Appends a subsequence of the character sequencecsqto the target. This method works the same way asPrintWriter.print(csq.subsequence(start, end).toString()). Ifcsqisnull, then the specified subsequence of the string "null" will be written to the target.- Specified by:
appendin interfaceAppendable- Overrides:
appendin classWriter- 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- ifstart > end,start < 0,end < 0or eitherstartorendare greater or equal than the length ofcsq.
-