PrintStream
adds functionality to another output stream,
namely the ability to print representations of various data values
conveniently. Two other features are provided as well. Unlike other output
streams, a PrintStream
never throws an
IOException
; instead, exceptional situations merely set an
internal flag that can be tested via the checkError
method.
Optionally, a PrintStream
can be created so as to flush
automatically; this means that the flush
method of the underlying
output stream is automatically invoked after a byte array is written, one
of the println
methods is invoked, or a newline character or byte
('\n'
) is written.
All characters printed by a PrintStream
are converted into
bytes using the given encoding or charset, or the default charset if not
specified.
The PrintWriter
class should be used in situations that require
writing characters rather than bytes.
This class always replaces malformed and unmappable character sequences with the charset's default replacement string. The java.nio.charset.CharsetEncoder class should be used when more control over the encoding process is required.
Charset#defaultCharset()
Modifier and Type | Field and Description |
---|---|
private final boolean | |
private OutputStreamWriter | |
private final Charset | |
private boolean | |
private Formatter | |
private final InternalLock | |
private BufferedWriter | textOut
Track both the text- and character-output streams, so that their buffers can be flushed without flushing the entire stream. |
private boolean |
Access | Constructor and Description |
---|---|
private | |
private | |
public | PrintStream(OutputStream
The output stream to which values and objects will be
printed out)Creates a new print stream, without automatic line flushing, with the specified OutputStream. |
public | PrintStream(OutputStream
The output stream to which values and objects will be
printed out, boolean Whether the output buffer will be flushed
whenever a byte array is written, one of the
autoFlush)println methods is invoked, or a newline
character or byte ('\n' ) is writtenCreates a new print stream, with the specified OutputStream and line flushing. |
public | PrintStream(OutputStream
The output stream to which values and objects will be
printed out, boolean Whether the output buffer will be flushed
whenever a byte array is written, one of the
autoFlush, String println methods is invoked, or a newline
character or byte ('\n' ) is writtenThe name of a supported
character encoding encoding)Creates a new print stream, with the specified OutputStream, line flushing, and character encoding. |
public | PrintStream(OutputStream
The output stream to which values and objects will be
printed out, boolean Whether the output buffer will be flushed
whenever a byte array is written, one of the
autoFlush, Charset println methods is invoked, or a newline
character or byte ('\n' ) is writtenA charset charset)Creates a new print stream, with the specified OutputStream, line flushing and charset. |
public | PrintStream(String
The name of the file to use as the destination of this print
stream. If the file exists, then it will be truncated to
zero size; otherwise, a new file will be created. The output
will be written to the file and is buffered. fileName)Creates a new print stream, without automatic line flushing, with the specified file name. |
public | PrintStream(String
The name of the file to use as the destination of this print
stream. If the file exists, then it will be truncated to
zero size; otherwise, a new file will be created. The output
will be written to the file and is buffered. fileName, String The name of a supported charset csn)Creates a new print stream, without automatic line flushing, with the specified file name and charset. |
public | PrintStream(String
The name of the file to use as the destination of this print
stream. If the file exists, then it will be truncated to
zero size; otherwise, a new file will be created. The output
will be written to the file and is buffered. fileName, Charset A charset charset)Creates a new print stream, without automatic line flushing, with the specified file name and charset. |
public | PrintStream(File
The file to use as the destination of this print stream. If the
file exists, then it will be truncated to zero size; otherwise,
a new file will be created. The output will be written to the
file and is buffered. file)Creates a new print stream, without automatic line flushing, with the specified file. |
public | PrintStream(File
The file to use as the destination of this print stream. If the
file exists, then it will be truncated to zero size; otherwise,
a new file will be created. The output will be written to the
file and is buffered. file, String The name of a supported charset csn)Creates a new print stream, without automatic line flushing, with the specified file and charset. |
public | PrintStream(File
The file to use as the destination of this print stream. If the
file exists, then it will be truncated to zero size; otherwise,
a new file will be created. The output will be written to the
file and is buffered. file, Charset A charset charset)Creates a new print stream, without automatic line flushing, with the specified file and charset. |
Modifier and Type | Method and Description |
---|---|
public PrintStream | Returns: This output streamThe character sequence to append. If csq)csq is
null , then the four characters "null" are
appended to this output stream.Implements java. Appends the specified character sequence to this output stream. |
public PrintStream | Returns: This output streamThe character sequence from which a subsequence will be
appended. If csq, int csq is null , then characters
will be appended as if csq contained the four
characters "null" .The index of the first character in the subsequence start, int The index of the character following the last character in the
subsequence end)Implements java. Appends a subsequence of the specified character sequence to this output stream. |
public PrintStream | Returns: This output streamThe 16-bit character to append c)Implements java. Appends the specified character to this output stream. |
public Charset | Returns: the charset used in thisPrintStream instanceReturns the charset used in this |
public boolean | Returns: true if and only if this stream has encountered an
IOException , or the setError method has been
invokedFlushes the stream if it's not closed and checks its error state. |
protected void | |
public void | close()
Overrides java. Implements java. Closes the stream. |
private void | |
public void | flush()
Overrides java. Implements java. Flushes the stream. |
public PrintStream | Returns: This output streamA format string as described in Format string syntax format, Object... Arguments referenced by the format specifiers in the format
string. If there are more arguments than format specifiers, the
extra arguments are ignored. The number of arguments is
variable and may be zero. The maximum number of arguments is
limited by the maximum dimension of a Java array as defined by
The Java Virtual Machine Specification.
The behaviour on a
args)null argument depends on the conversion.Writes a formatted string to this output stream using the specified format string and arguments. |
public PrintStream | Returns: This output streamA format string as described in Format string syntax format, Object... Arguments referenced by the format specifiers in the format
string. If there are more arguments than format specifiers, the
extra arguments are ignored. The number of arguments is
variable and may be zero. The maximum number of arguments is
limited by the maximum dimension of a Java array as defined by
The Java Virtual Machine Specification.
The behaviour on a
args)null argument depends on the conversion.Writes a formatted string to this output stream using the specified format string and arguments. |
private void | |
private void | |
private void | |
private void | |
private void | |
private void | |
private void | |
private void | |
private void | |
private void | |
private void | |
private void | |
public void | |
public void | |
public void | |
public void | |
public void | |
public void | |
public void | |
public void | |
public void | |
public PrintStream | Returns: This output streamA format string as described in Format string syntax format, Object... Arguments referenced by the format specifiers in the format
string. If there are more arguments than format specifiers, the
extra arguments are ignored. The number of arguments is
variable and may be zero. The maximum number of arguments is
limited by the maximum dimension of a Java array as defined by
The Java Virtual Machine Specification.
The behaviour on a
args)null argument depends on the conversion.A convenience method to write a formatted string to this output stream using the specified format string and arguments. |
public PrintStream | Returns: This output streamA format string as described in Format string syntax format, Object... Arguments referenced by the format specifiers in the format
string. If there are more arguments than format specifiers, the
extra arguments are ignored. The number of arguments is
variable and may be zero. The maximum number of arguments is
limited by the maximum dimension of a Java array as defined by
The Java Virtual Machine Specification.
The behaviour on a
args)null argument depends on the conversion.A convenience method to write a formatted string to this output stream using the specified format string and arguments. |
public void | |
public void | |
public void | |
public void | |
public void | |
public void | |
public void | |
public void | println(char[]
an array of chars to print. x)Prints an array of characters and then terminates the line. |
public void | |
public void | |
private static <T> T | requireNonNull(T obj, String message)
requireNonNull is explicitly declared here so as not to create an extra dependency on java.util.Objects.requireNonNull. |
protected void | |
private static Charset | |
public void | write(int
The byte to be written b)Overrides java. Writes the specified byte to this stream. |
public void | write(byte[]
A byte array buf, int Offset from which to start taking bytes off, int Number of bytes to write len)Overrides java. Writes |
public void | write(byte[]
A byte array buf)Overrides java. Writes all bytes from the specified byte array to this stream. |
private void | |
private void | |
public void | |
private void | |
private void |
autoFlush | back to summary |
---|---|
private final boolean autoFlush |
charOut | back to summary |
---|---|
private OutputStreamWriter charOut |
charset | back to summary |
---|---|
private final Charset charset |
closing | back to summary |
---|---|
private boolean closing |
formatter | back to summary |
---|---|
private Formatter formatter |
lock | back to summary |
---|---|
private final InternalLock lock |
textOut | back to summary |
---|---|
private BufferedWriter textOut Track both the text- and character-output streams, so that their buffers can be flushed without flushing the entire stream. |
trouble | back to summary |
---|---|
private boolean trouble |
PrintStream | back to summary |
---|---|
private PrintStream(boolean autoFlush, OutputStream out) |
PrintStream | back to summary |
---|---|
private PrintStream(boolean autoFlush, Charset charset, OutputStream out) |
PrintStream | back to summary |
---|---|
public PrintStream(OutputStream out) Creates a new print stream, without automatic line flushing, with the
specified OutputStream. Characters written to the stream are converted
to bytes using the default charset, or where
|
PrintStream | back to summary |
---|---|
public PrintStream(OutputStream out, boolean autoFlush) Creates a new print stream, with the specified OutputStream and line
flushing. Characters written to the stream are converted to bytes using
the default charset, or where
|
PrintStream | back to summary |
---|---|
public PrintStream(OutputStream out, boolean autoFlush, String encoding) throws UnsupportedEncodingException Creates a new print stream, with the specified OutputStream, line flushing, and character encoding.
|
PrintStream | back to summary |
---|---|
public PrintStream(OutputStream out, boolean autoFlush, Charset charset) Creates a new print stream, with the specified OutputStream, line
flushing and charset. This convenience constructor creates the necessary
intermediate
|
PrintStream | back to summary |
---|---|
public PrintStream(String fileName) throws FileNotFoundException Creates a new print stream, without automatic line flushing, with the
specified file name. This convenience constructor creates
the necessary intermediate
|
PrintStream | back to summary |
---|---|
public PrintStream(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException Creates a new print stream, without automatic line flushing, with the
specified file name and charset. This convenience constructor creates
the necessary intermediate
|
PrintStream | back to summary |
---|---|
public PrintStream(String fileName, Charset charset) throws IOException Creates a new print stream, without automatic line flushing, with the
specified file name and charset. This convenience constructor creates
the necessary intermediate
|
PrintStream | back to summary |
---|---|
public PrintStream(File file) throws FileNotFoundException Creates a new print stream, without automatic line flushing, with the
specified file. This convenience constructor creates the necessary
intermediate
|
PrintStream | back to summary |
---|---|
public PrintStream(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException Creates a new print stream, without automatic line flushing, with the
specified file and charset. This convenience constructor creates
the necessary intermediate
|
PrintStream | back to summary |
---|---|
public PrintStream(File file, Charset charset) throws IOException Creates a new print stream, without automatic line flushing, with the
specified file and charset. This convenience constructor creates
the necessary intermediate
|
append | back to summary |
---|---|
public PrintStream append(CharSequence csq) Implements java. Appends the specified character sequence to this output stream. An invocation of this method of the form out.print(csq.toString()) Depending on the specification of
|
append | back to summary |
---|---|
public PrintStream append(CharSequence csq, int start, int end) Implements java. Appends a subsequence of the specified character sequence to this output stream. An invocation of this method of the form
out.print(csq.subSequence(start, end).toString())
|
append | back to summary |
---|---|
public PrintStream append(char c) Implements java. Appends the specified character to this output stream. An invocation of this method of the form out.print(c)
|
charset | back to summary |
---|---|
public Charset charset() Returns the charset used in this
|
checkError | back to summary |
---|---|
public boolean checkError() Flushes the stream if it's not closed and checks its error state.
|
clearError | back to summary |
---|---|
protected void clearError() Clears the error state of this stream. This method will cause subsequent invocations of
|
close | back to summary |
---|---|
public void close() Overrides java. Implements java. Closes the stream. This is done by flushing the stream and then closing the underlying output stream.
|
ensureOpen | back to summary |
---|---|
private void ensureOpen() throws IOException Check to make sure that the stream has not been closed |
flush | back to summary |
---|---|
public void flush() Overrides java. Implements java. Flushes the stream. This is done by writing any buffered output bytes to the underlying output stream and then flushing that stream.
|
format | back to summary |
---|---|
public PrintStream format(String format, Object... args) Writes a formatted string to this output stream using the specified format string and arguments. The locale always used is the one returned by
|
format | back to summary |
---|---|
public PrintStream format(Locale l, String format, Object... args) Writes a formatted string to this output stream using the specified format string and arguments.
|
implClose | back to summary |
---|---|
private void implClose() |
implFlush | back to summary |
---|---|
private void implFlush() |
implFormat | back to summary |
---|---|
private void implFormat(String format, Object... args) throws IOException |
implFormat | back to summary |
---|---|
private void implFormat(Locale l, String format, Object... args) throws IOException |
implNewLine | back to summary |
---|---|
private void implNewLine() throws IOException |
implWrite | back to summary |
---|---|
private void implWrite(int b) throws IOException |
implWrite | back to summary |
---|---|
private void implWrite(byte[] buf, int off, int len) throws IOException |
implWrite | back to summary |
---|---|
private void implWrite(char[] buf) throws IOException |
implWrite | back to summary |
---|---|
private void implWrite(String s) throws IOException |
implWriteln | back to summary |
---|---|
private void implWriteln(char[] buf) throws IOException |
implWriteln | back to summary |
---|---|
private void implWriteln(String s) throws IOException |
newLine | back to summary |
---|---|
private void newLine() |
back to summary | |
---|---|
public void print(boolean b) Prints a boolean value. The string produced by
|
back to summary | |
---|---|
public void print(char c) Prints a character. The character is translated into one or more bytes
according to the character encoding given to the constructor, or the
default charset if none specified. These bytes
are written in exactly the manner of the
|
back to summary | |
---|---|
public void print(int i) Prints an integer. The string produced by
|
back to summary | |
---|---|
public void print(long l) Prints a long integer. The string produced by
|
back to summary | |
---|---|
public void print(float f) Prints a floating-point number. The string produced by
|
back to summary | |
---|---|
public void print(double d) Prints a double-precision floating-point number. The string produced by
|
back to summary | |
---|---|
public void print(char[] s) Prints an array of characters. The characters are converted into bytes
according to the character encoding given to the constructor, or the
default charset if none specified. These bytes
are written in exactly the manner of the
|
back to summary | |
---|---|
public void print(String s) Prints a string. If the argument is
|
back to summary | |
---|---|
public void print(Object obj) Prints an object. The string produced by the
|
printf | back to summary |
---|---|
public PrintStream printf(String format, Object... args) A convenience method to write a formatted string to this output stream using the specified format string and arguments. An invocation of this method of the form
out.format(format, args)
|
printf | back to summary |
---|---|
public PrintStream printf(Locale l, String format, Object... args) A convenience method to write a formatted string to this output stream using the specified format string and arguments. An invocation of this method of the form
out.format(l, format, args)
|
println | back to summary |
---|---|
public void println() Terminates the current line by writing the line separator string. The
line separator string is defined by the system property
|
println | back to summary |
---|---|
public void println(boolean x) Prints a boolean and then terminates the line. This method behaves as
though it invokes
|
println | back to summary |
---|---|
public void println(char x) Prints a character and then terminates the line. This method behaves as
though it invokes
|
println | back to summary |
---|---|
public void println(int x) Prints an integer and then terminates the line. This method behaves as
though it invokes
|
println | back to summary |
---|---|
public void println(long x) Prints a long and then terminates the line. This method behaves as
though it invokes
|
println | back to summary |
---|---|
public void println(float x) Prints a float and then terminates the line. This method behaves as
though it invokes
|
println | back to summary |
---|---|
public void println(double x) Prints a double and then terminates the line. This method behaves as
though it invokes
|
println | back to summary |
---|---|
public void println(char[] x) Prints an array of characters and then terminates the line. This method
behaves as though it invokes
|
println | back to summary |
---|---|
public void println(String x) Prints a String and then terminates the line. This method behaves as
though it invokes
|
println | back to summary |
---|---|
public void println(Object x) Prints an Object and then terminates the line. This method calls
at first String.valueOf(x) to get the printed object's string value,
then behaves as
though it invokes
|
requireNonNull | back to summary |
---|---|
private static <T> T requireNonNull(T obj, String message) requireNonNull is explicitly declared here so as not to create an extra dependency on java.util.Objects.requireNonNull. PrintStream is loaded early during system initialization. |
setError | back to summary |
---|---|
protected void setError() Sets the error state of the stream to This method will cause subsequent invocations of
|
toCharset | back to summary |
---|---|
private static Charset toCharset(String csn) throws UnsupportedEncodingException Returns a charset object for the given charset name.
|
write | back to summary |
---|---|
public void write(int b) Overrides java. Writes the specified byte to this stream. If the byte is a newline and
automatic flushing is enabled then the Note that the byte is written as given; to write a character that
will be translated according to the default charset, use the
|
write | back to summary |
---|---|
public void write(byte[] buf, int off, int len) Overrides java. Writes Note that the bytes will be written as given; to write characters
that will be translated according to the default charset, use the
|
write | back to summary |
---|---|
public void write(byte[] buf) throws IOException Overrides java. Writes all bytes from the specified byte array to this stream. If
automatic flushing is enabled then the Note that the bytes will be written as given; to write characters
that will be translated according to the default charset, use the
API Note Although declared to throw Implementation Specification This method is equivalent to
|
write | back to summary |
---|---|
private void write(char[] buf) |
write | back to summary |
---|---|
private void write(String s) |
writeBytes | back to summary |
---|---|
public void writeBytes(byte[] buf) Writes all bytes from the specified byte array to this stream.
If automatic flushing is enabled then the Note that the bytes will be written as given; to write characters
that will be translated according to the default charset, use the
Implementation Specification This method is equivalent to
|
writeln | back to summary |
---|---|
private void writeln(char[] buf) |
writeln | back to summary |
---|---|
private void writeln(String s) |