Package java.io
Class PrintStream
java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
java.io.PrintStream
- All Implemented Interfaces:
Closeable,Flushable,Appendable,AutoCloseable
- Direct Known Subclasses:
Logger.Stream
public class PrintStream extends FilterOutputStream implements Appendable, Closeable
Wraps an existing
OutputStream and provides convenience methods for
writing common data types in a human readable format. This is not to be
confused with DataOutputStream which is used for encoding common data types
so that they can be read back in. No IOException is thrown by this
class. Instead, callers should use checkError() to see if a problem
has occurred in this stream.-
Field Summary
Fields inherited from class java.io.FilterOutputStream
out -
Constructor Summary
Constructors Constructor Description PrintStream(File file)Constructs a newPrintStreamwithfileas its target.PrintStream(File file, String charsetName)Constructs a newPrintStreamwithfileas its target.PrintStream(OutputStream out)Constructs a newPrintStreamwithoutas its target stream.PrintStream(OutputStream out, boolean autoFlush)Constructs a newPrintStreamwithoutas its target stream.PrintStream(OutputStream out, boolean autoFlush, String charsetName)Constructs a newPrintStreamwithoutas its target stream and using the character encodingcharsetNamewhile writing.PrintStream(String fileName)Constructs a newPrintStreamwith the file identified byfileNameas its target.PrintStream(String fileName, String charsetName)Constructs a newPrintStreamwith the file identified byfileNameas its target. -
Method Summary
Modifier and Type Method Description PrintStreamappend(char c)Appends the charc.PrintStreamappend(CharSequence charSequence)Appends the CharSequencecharSequence, or"null".PrintStreamappend(CharSequence charSequence, int start, int end)Appends a subsequence of CharSequencecharSequence, or"null".booleancheckError()Flushes this stream and returns the value of the error flag.protected voidclearError()Sets the error state of the stream to false.voidclose()Closes this print stream.voidflush()Ensures that all pending data is sent out to the target stream.PrintStreamformat(String format, Object... args)Formatsargsaccording to the format stringformat, and writes the result to this stream.PrintStreamformat(Locale l, String format, Object... args)Writes a string formatted by an intermediateFormatterto this stream using the specified locale, format string and arguments.voidprint(boolean b)Prints the string representation of the booleanb.voidprint(char c)Prints the string representation of the charc.voidprint(char[] chars)Prints the string representation of the character arraychars.voidprint(double d)Prints the string representation of the doubled.voidprint(float f)Prints the string representation of the floatf.voidprint(int i)Prints the string representation of the inti.voidprint(long l)Prints the string representation of the longl.voidprint(Object o)Prints the string representation of the Objecto, or"null".voidprint(String str)Prints a string to the target stream.PrintStreamprintf(String format, Object... args)Prints a formatted string.PrintStreamprintf(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 o)Prints the string representation of the Objecto, or"null", followed by a newline.voidprintln(String str)Prints a string followed by a newline.protected voidsetError()Sets the error flag of this print stream to true.voidwrite(byte[] buffer, int offset, int length)Writescountbytes frombufferstarting atoffsetto the target stream.voidwrite(int oneByte)Writes one byte to the target stream.Methods inherited from class java.io.OutputStream
write
-
Constructor Details
-
PrintStream
Constructs a newPrintStreamwithoutas its target stream. By default, the new print stream does not automatically flush its contents to the target stream when a newline is encountered.- Parameters:
out- the target output stream.- Throws:
NullPointerException- ifoutisnull.
-
PrintStream
Constructs a newPrintStreamwithoutas its target stream. The parameterautoFlushdetermines if the print stream automatically flushes its contents to the target stream when a newline is encountered.- Parameters:
out- the target output stream.autoFlush- indicates whether to flush contents upon encountering a newline sequence.- Throws:
NullPointerException- ifoutisnull.
-
PrintStream
public PrintStream(OutputStream out, boolean autoFlush, String charsetName) throws UnsupportedEncodingExceptionConstructs a newPrintStreamwithoutas its target stream and using the character encodingcharsetNamewhile writing. The parameterautoFlushdetermines if the print stream automatically flushes its contents to the target stream when a newline is encountered.- Parameters:
out- the target output stream.autoFlush- indicates whether or not to flush contents upon encountering a newline sequence.charsetName- the non-null string describing the desired character encoding.- Throws:
NullPointerException- ifoutorcharsetNamearenull.UnsupportedEncodingException- if the encoding specified bycharsetNameis not supported.
-
PrintStream
Constructs a newPrintStreamwithfileas its target. The VM's default character set is used for character encoding.- 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.
-
PrintStream
public PrintStream(File file, String charsetName) throws FileNotFoundException, UnsupportedEncodingExceptionConstructs a newPrintStreamwithfileas its target. The character set namedcharsetNameis used for character encoding.- Parameters:
file- the target file. If the file already exists, its contents are removed, otherwise a new file is created.charsetName- the name of the character set used for character encoding.- Throws:
FileNotFoundException- if an error occurs while opening or creating the target file.NullPointerException- ifcharsetNameisnull.UnsupportedEncodingException- if the encoding specified bycharsetNameis not supported.
-
PrintStream
Constructs a newPrintStreamwith the file identified byfileNameas its target. The VM's default character set is used for character encoding.- 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.
-
PrintStream
public PrintStream(String fileName, String charsetName) throws FileNotFoundException, UnsupportedEncodingExceptionConstructs a newPrintStreamwith the file identified byfileNameas its target. The character set namedcharsetNameis used for character encoding.- Parameters:
fileName- the target file's name. If the file already exists, its contents are removed, otherwise a new file is created.charsetName- the name of the character set used for character encoding.- Throws:
FileNotFoundException- if an error occurs while opening or creating the target file.NullPointerException- ifcharsetNameisnull.UnsupportedEncodingException- if the encoding specified bycharsetNameis not supported.
-
-
Method Details
-
checkError
public boolean checkError()Flushes this stream 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 stream. Flushes this stream and then closes the target stream. If an I/O error occurs, this stream's error state is set totrue.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classFilterOutputStream
-
flush
public void flush()Ensures that all pending data is sent out to the target stream. It also flushes the target stream. If an I/O error occurs, this stream's error state is set totrue.- Specified by:
flushin interfaceFlushable- Overrides:
flushin classFilterOutputStream
-
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".- 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 stream.
- 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 this stream using the specified locale, format string and arguments.- 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 stream.
- 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 stream'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 stream.
- 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 stream'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 stream.
- 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[] chars)Prints the string representation of the character arraychars. -
print
public void print(char c)Prints the string representation of the charc. -
print
public void print(double d)Prints the string representation of the doubled. -
print
public void print(float f)Prints the string representation of the floatf. -
print
public void print(int i)Prints the string representation of the inti. -
print
public void print(long l)Prints the string representation of the longl. -
print
Prints the string representation of the Objecto, or"null". -
print
Prints a string to the target stream. The string is converted to an array of bytes using the encoding chosen during the construction of this stream. The bytes are then written to the target stream withwrite(int).If an I/O error occurs, this stream's error state is set to
true.- Parameters:
str- the string to print to the target stream.- See Also:
write(int)
-
print
public void print(boolean b)Prints the string representation of the booleanb. -
println
public void println()Prints a newline. -
println
public void println(char[] chars)Prints the string representation of the character arraycharsfollowed by a newline. -
println
public void println(char c)Prints the string representation of the charcfollowed by a newline. -
println
public void println(double d)Prints the string representation of the doubledfollowed by a newline. -
println
public void println(float f)Prints the string representation of the floatffollowed by a newline. -
println
public void println(int i)Prints the string representation of the intifollowed by a newline. -
println
public void println(long l)Prints the string representation of the longlfollowed by a newline. -
println
Prints the string representation of the Objecto, or"null", followed by a newline. -
println
Prints a string followed by a newline. The string is converted to an array of bytes using the encoding chosen during the construction of this stream. The bytes are then written to the target stream withwrite(int).If an I/O error occurs, this stream's error state is set to
true.- Parameters:
str- the string to print to the target stream.- See Also:
write(int)
-
println
public void println(boolean b)Prints the string representation of the booleanbfollowed by a newline. -
setError
protected void setError()Sets the error flag of this print stream to true. -
write
public void write(byte[] buffer, int offset, int length)Writescountbytes frombufferstarting atoffsetto the target stream. If autoFlush is set, this stream gets flushed after writing the buffer.This stream's error flag is set to
trueif this stream is closed or an I/O error occurs.- Overrides:
writein classFilterOutputStream- Parameters:
buffer- the buffer to be written.offset- the index of the first byte inbufferto write.length- the number of bytes inbufferto write.- Throws:
IndexOutOfBoundsException- ifoffset < 0orcount < 0, or ifoffset + countis bigger than the length ofbuffer.- See Also:
flush()
-
write
public void write(int oneByte)Writes one byte to the target stream. Only the least significant byte of the integeroneByteis written. This stream is flushed ifoneByteis equal to the character'\n'and this stream is set to autoFlush.This stream's error flag is set to
trueif it is closed or an I/O error occurs.- Overrides:
writein classFilterOutputStream- Parameters:
oneByte- the byte to be written
-
append
Appends the charc.- Specified by:
appendin interfaceAppendable- Parameters:
c- the character to append.- Returns:
- this stream.
-
append
Appends the CharSequencecharSequence, or"null".- Specified by:
appendin interfaceAppendable- Parameters:
charSequence- the character sequence to append.- Returns:
- this stream.
-
append
Appends a subsequence of CharSequencecharSequence, or"null".- Specified by:
appendin interfaceAppendable- Parameters:
charSequence- the character sequence appended to the target stream.start- the index of the first char in the character sequence appended to the target stream.end- the index of the character following the last character of the subsequence appended to the target stream.- Returns:
- this stream.
- Throws:
IndexOutOfBoundsException- ifstart > end,start < 0,end < 0or eitherstartorendare greater or equal than the length ofcharSequence.
-