public class PrintStream extends FilterOutputStream implements Appendable, Closeable
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.out| Constructor and Description |
|---|
PrintStream(File file)
Creates a new print stream, without automatic line flushing, with the
specified file.
|
PrintStream(File file,
Charset charset)
Creates a new print stream, without automatic line flushing, with the
specified file and charset.
|
PrintStream(File file,
String csn)
Creates a new print stream, without automatic line flushing, with the
specified file and charset.
|
PrintStream(OutputStream out)
Creates a new print stream.
|
PrintStream(OutputStream out,
boolean autoFlush)
Creates a new print stream.
|
PrintStream(OutputStream out,
boolean autoFlush,
Charset charset)
Creates a new print stream, with the specified OutputStream, automatic line
flushing and charset.
|
PrintStream(OutputStream out,
boolean autoFlush,
String encoding)
Creates a new print stream.
|
PrintStream(String fileName)
Creates a new print stream, without automatic line flushing, with the
specified file name.
|
PrintStream(String fileName,
Charset charset)
Creates a new print stream, without automatic line flushing, with the
specified file name and charset.
|
PrintStream(String fileName,
String csn)
Creates a new print stream, without automatic line flushing, with the
specified file name and charset.
|
| Modifier and Type | Method and Description |
|---|---|
PrintStream |
append(char c)
Appends the char
c. |
PrintStream |
append(CharSequence charSequence)
Appends the CharSequence
charSequence, or "null". |
PrintStream |
append(CharSequence charSequence,
int start,
int end)
Appends a subsequence of CharSequence
charSequence, or "null". |
boolean |
checkError()
Flushes this stream 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 stream.
|
void |
flush()
Ensures that all pending data is sent out to the target stream.
|
PrintStream |
format(Locale l,
String format,
Object... args)
Writes a string formatted by an intermediate
Formatter to this
stream using the specified locale, format string and arguments. |
PrintStream |
format(String format,
Object... args)
Formats
args according to the format string format, and writes the result
to this stream. |
void |
print(boolean b)
Prints the string representation of the boolean
b. |
void |
print(char c)
Prints the string representation of the char
c. |
void |
print(char[] chars)
Prints the string representation of the character array
chars. |
void |
print(double d)
Prints the string representation of the double
d. |
void |
print(float f)
Prints the string representation of the float
f. |
void |
print(int i)
Prints the string representation of the int
i. |
void |
print(long l)
Prints the string representation of the long
l. |
void |
print(Object o)
Prints the string representation of the Object
o, or "null". |
void |
print(String str)
Prints a string to the target stream.
|
PrintStream |
printf(Locale l,
String format,
Object... args)
Prints a formatted string.
|
PrintStream |
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 o)
Prints the string representation of the Object
o, or "null",
followed by a newline. |
void |
println(String str)
Prints a string followed by a newline.
|
protected void |
setError()
Sets the error flag of this print stream to true.
|
void |
write(byte[] buffer,
int offset,
int length)
Writes
count bytes from buffer starting at offset
to the target stream. |
void |
write(int oneByte)
Writes one byte to the target stream.
|
nullOutputStream, writepublic PrintStream(OutputStream out)
out - The output stream to which values and objects will be
printedPrintWriter(java.io.OutputStream)public PrintStream(OutputStream out, boolean autoFlush)
out - The output stream to which values and objects will be
printedautoFlush - A boolean; if true, the output buffer will be flushed
whenever a byte array is written, one of the
println methods is invoked, or a newline
character or byte ('\n') is writtenPrintWriter(java.io.OutputStream, boolean)public PrintStream(OutputStream out, boolean autoFlush, String encoding) throws UnsupportedEncodingException
out - The output stream to which values and objects will be
printedautoFlush - A boolean; if true, the output buffer will be flushed
whenever a byte array is written, one of the
println methods is invoked, or a newline
character or byte ('\n') is writtenencoding - The name of a supported
character encodingUnsupportedEncodingException - If the named encoding is not supportedpublic PrintStream(OutputStream out, boolean autoFlush, Charset charset)
OutputStreamWriter,
which will encode characters using the provided charset.out - The output stream to which values and objects will be
printedautoFlush - A boolean; if true, the output buffer will be flushed
whenever a byte array is written, one of the
println methods is invoked, or a newline
character or byte ('\n') is writtencharset - A charsetpublic PrintStream(String fileName) throws FileNotFoundException
OutputStreamWriter, which will encode characters using the
default charset
for this instance of the Java virtual machine.fileName - 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.FileNotFoundException - If the given file object does not denote an existing, writable
regular file and a new regular file of that name cannot be
created, or if some other error occurs while opening or
creating the fileSecurityException - If a security manager is present and checkWrite(fileName) denies write
access to the filepublic PrintStream(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException
OutputStreamWriter, which will encode characters using the provided
charset.fileName - 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.csn - The name of a supported charsetFileNotFoundException - If the given file object does not denote an existing, writable
regular file and a new regular file of that name cannot be
created, or if some other error occurs while opening or
creating the fileSecurityException - If a security manager is present and checkWrite(fileName) denies write
access to the fileUnsupportedEncodingException - If the named charset is not supportedpublic PrintStream(String fileName, Charset charset) throws IOException
OutputStreamWriter, which will encode characters using the provided
charset.fileName - 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.charset - A charsetIOException - if an I/O error occurs while opening or creating the fileSecurityException - If a security manager is present and checkWrite(fileName) denies write
access to the filepublic PrintStream(File file) throws FileNotFoundException
OutputStreamWriter,
which will encode characters using the default charset for this
instance of the Java virtual machine.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.FileNotFoundException - If the given file object does not denote an existing, writable
regular file and a new regular file of that name cannot be
created, or if some other error occurs while opening or
creating the fileSecurityException - If a security manager is present and checkWrite(file.getPath())
denies write access to the filepublic PrintStream(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException
OutputStreamWriter, which will encode characters using the provided
charset.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.csn - The name of a supported charsetFileNotFoundException - If the given file object does not denote an existing, writable
regular file and a new regular file of that name cannot be
created, or if some other error occurs while opening or
creating the fileSecurityException - If a security manager is present and checkWrite(file.getPath())
denies write access to the fileUnsupportedEncodingException - If the named charset is not supportedpublic PrintStream(File file, Charset charset) throws IOException
OutputStreamWriter, which will encode characters using the provided
charset.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.charset - A charsetIOException - if an I/O error occurs while opening or creating the fileSecurityException - If a security manager is present and checkWrite(file.getPath())
denies write access to the filepublic 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.close in interface Closeableclose in interface AutoCloseableclose in class FilterOutputStreampublic void flush()
true.flush in interface Flushableflush in class FilterOutputStreampublic PrintStream 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".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 == nullpublic PrintStream format(Locale l, String format, Object... args)
Formatter to this
stream using the specified locale, format string and arguments.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 == nullpublic PrintStream 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 == nullpublic PrintStream 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[] chars)
chars.public void print(char c)
c.public void print(double d)
d.public void print(float f)
f.public void print(int i)
i.public void print(long l)
l.public void print(Object o)
o, or "null".public void print(String str)
write(int).
If an I/O error occurs, this stream's error state is set to true.
str - the string to print to the target stream.write(int)public void print(boolean b)
b.public void println()
public void println(char[] chars)
chars followed by a newline.public void println(char c)
c followed by a newline.public void println(double d)
d followed by a newline.public void println(float f)
f followed by a newline.public void println(int i)
i followed by a newline.public void println(long l)
l followed by a newline.public void println(Object o)
o, or "null",
followed by a newline.public void println(String str)
write(int).
If an I/O error occurs, this stream's error state is set to true.
str - the string to print to the target stream.write(int)public void println(boolean b)
b followed by a newline.protected void setError()
public void write(byte[] buffer,
int offset,
int length)
count bytes from buffer starting at offset
to the target stream. If autoFlush is set, this stream gets flushed after
writing the buffer.
This stream's error flag is set to true if this stream is closed
or an I/O error occurs.
write in class FilterOutputStreambuffer - the buffer to be written.offset - the index of the first byte in buffer to write.length - the number of bytes in buffer to write.IndexOutOfBoundsException - if offset < 0 or count < 0, or if offset + count is bigger than the length of buffer.flush()public void write(int oneByte)
oneByte is written. This stream is flushed if
oneByte is equal to the character '\n' and this stream is
set to autoFlush.
This stream's error flag is set to true if it is closed or an I/O
error occurs.
write in class FilterOutputStreamoneByte - the byte to be writtenpublic PrintStream append(char c)
c.append in interface Appendablec - the character to append.public PrintStream append(CharSequence charSequence)
charSequence, or "null".append in interface AppendablecharSequence - the character sequence to append.public PrintStream append(CharSequence charSequence, int start, int end)
charSequence, or "null".append in interface AppendablecharSequence - 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.IndexOutOfBoundsException - if start > end, start < 0, end < 0 or
either start or end are greater or equal than
the length of charSequence.