public class

PrintStream

extends FilterOutputStream
implements Closeable Appendable
java.lang.Object
   ↳ java.io.OutputStream
     ↳ java.io.FilterOutputStream
       ↳ java.io.PrintStream

Class Overview

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.

Summary

[Expand]
Inherited Fields
From class java.io.FilterOutputStream
Public Constructors
PrintStream(OutputStream out)
Constructs a new PrintStream with out as its target stream.
PrintStream(OutputStream out, boolean autoflush)
Constructs a new PrintStream with out as its target stream.
PrintStream(OutputStream out, boolean autoflush, String enc)
Constructs a new PrintStream with out as its target stream and using the character encoding enc while writing.
PrintStream(File file)
Constructs a new PrintStream with file as its target.
PrintStream(File file, String csn)
Constructs a new PrintStream with file as its target.
PrintStream(String fileName)
Constructs a new PrintStream with the file identified by fileName as its target.
PrintStream(String fileName, String csn)
Constructs a new PrintStream with the file identified by fileName as its target.
Public Methods
PrintStream append(char c)
Appends the character c to the target stream.
PrintStream append(CharSequence csq, int start, int end)
Appends a subsequence of the character sequence csq to the target stream.
PrintStream append(CharSequence csq)
Appends the character sequence csq to the target stream.
boolean checkError()
Flushes this stream and returns the value of the error flag.
synchronized void close()
Closes this print stream.
synchronized 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(float fnum)
Prints the string representation of the specified float to the target stream.
void print(double dnum)
Prints the string representation of the specified double to the target stream.
synchronized void print(String str)
Prints a string to the target stream.
void print(Object obj)
Prints the string representation of the specified object to the target stream.
void print(char ch)
Prints the string representation of the specified character to the target stream.
void print(char[] charArray)
Prints the string representation of the specified character array to the target stream.
void print(long lnum)
Prints the string representation of the specified long to the target stream.
void print(int inum)
Prints the string representation of the specified integer to the target stream.
void print(boolean bool)
Prints the string representation of the specified boolean 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 the string representation of the system property "line.separator" to the target stream.
void println(float fnum)
Prints the string representation of the specified float followed by the system property "line.separator" to the target stream.
void println(int inum)
Prints the string representation of the specified integer followed by the system property "line.separator" to the target stream.
void println(long lnum)
Prints the string representation of the specified long followed by the system property "line.separator" to the target stream.
void println(Object obj)
Prints the string representation of the specified object followed by the system property "line.separator" to the target stream.
void println(char[] charArray)
Prints the string representation of the specified character array followed by the system property "line.separator" to the target stream.
synchronized void println(String str)
Prints a string followed by the system property "line.separator" to the target stream.
void println(char ch)
Prints the string representation of the specified character followed by the system property "line.separator" to the target stream.
void println(double dnum)
Prints the string representation of the specified double followed by the system property "line.separator" to the target stream.
void println(boolean bool)
Prints the string representation of the specified boolean followed by the system property "line.separator" to the target stream.
synchronized void write(int oneByte)
Writes one byte to the target stream.
void write(byte[] buffer, int offset, int length)
Writes count bytes from buffer starting at offset to the target stream.
Protected Methods
void clearError()
Sets the error state of the stream to false.
void setError()
Sets the error flag of this print stream to true.
[Expand]
Inherited Methods
From class java.io.FilterOutputStream
From class java.io.OutputStream
From class java.lang.Object
From interface java.io.Closeable
From interface java.io.Flushable
From interface java.lang.Appendable

Public Constructors

public PrintStream (OutputStream out)

Since: API Level 1

Constructs a new PrintStream with out as 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 if out is null.

public PrintStream (OutputStream out, boolean autoflush)

Since: API Level 1

Constructs a new PrintStream with out as its target stream. The parameter autoflush determines 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 if out is null.

public PrintStream (OutputStream out, boolean autoflush, String enc)

Since: API Level 1

Constructs a new PrintStream with out as its target stream and using the character encoding enc while writing. The parameter autoflush determines 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.
enc the non-null string describing the desired character encoding.
Throws
NullPointerException if out or enc are null.
UnsupportedEncodingException if the encoding specified by enc is not supported.

public PrintStream (File file)

Since: API Level 1

Constructs a new PrintStream with file as its target. The virtual machine'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.
SecurityException if a security manager exists and it denies writing to the target file.

public PrintStream (File file, String csn)

Since: API Level 1

Constructs a new PrintStream with file as its target. The character set named csn 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.
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 if csn is null.
SecurityException if a security manager exists and it denies writing to the target file.
UnsupportedEncodingException if the encoding specified by csn is not supported.

public PrintStream (String fileName)

Since: API Level 1

Constructs a new PrintStream with the file identified by fileName as its target. The virtual machine'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.
SecurityException if a security manager exists and it denies writing to the target file.

public PrintStream (String fileName, String csn)

Since: API Level 1

Constructs a new PrintStream with the file identified by fileName as its target. The character set named csn 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.
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 if csn is null.
SecurityException if a security manager exists and it denies writing to the target file.
UnsupportedEncodingException if the encoding specified by csn is not supported.

Public Methods

public PrintStream append (char c)

Since: API Level 1

Appends the character c to the target stream. This method works the same way as print(char).

Parameters
c the character to append to the target stream.
Returns
  • this stream.

public PrintStream append (CharSequence csq, int start, int end)

Since: API Level 1

Appends a subsequence of the character sequence csq to the target stream. This method works the same way as PrintStream.print(csq.subsequence(start, end).toString()). If csq is null, then the specified subsequence of the string "null" will be written to the target stream.

Parameters
csq 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 if start > end, start < 0, end < 0 or either start or end are greater or equal than the length of csq.

public PrintStream append (CharSequence csq)

Since: API Level 1

Appends the character sequence csq to the target stream. This method works the same way as PrintStream.print(csq.toString()). If csq is null, then the string "null" is written to the target stream.

Parameters
csq the character sequence appended to the target stream.
Returns
  • this stream.

public boolean checkError ()

Since: API Level 1

Flushes this stream and returns the value of the error flag.

Returns
  • true if either an IOException has been thrown previously or if setError() has been called; false otherwise.
See Also

public synchronized void close ()

Since: API Level 1

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 to true.

public synchronized void flush ()

Since: API Level 1

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 to true.

public PrintStream format (Locale l, String format, Object... args)

Since: API Level 1

Writes a string formatted by an intermediate Formatter to this stream using the specified locale, format string and arguments.

Parameters
l the locale used in the method. No localization will be applied if l is null.
format the format string (see format(String, Object...))
args the list of arguments passed to the formatter. If there are more arguments than required by format, 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 if format == null

public PrintStream format (String format, Object... args)

Since: API Level 1

Formats 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".

Parameters
format the format string (see format(String, Object...))
args the list of arguments passed to the formatter. If there are more arguments than required by format, 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 if format == null

public void print (float fnum)

Since: API Level 1

Prints the string representation of the specified float to the target stream.

Parameters
fnum the float value to print to the target stream.
See Also

public void print (double dnum)

Since: API Level 1

Prints the string representation of the specified double to the target stream.

Parameters
dnum the double value to print to the target stream.
See Also

public synchronized void print (String str)

Since: API Level 1

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 with write(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

public void print (Object obj)

Since: API Level 1

Prints the string representation of the specified object to the target stream.

Parameters
obj the object to print to the target stream.
See Also

public void print (char ch)

Since: API Level 1

Prints the string representation of the specified character to the target stream.

Parameters
ch the character to print to the target stream.
See Also

public void print (char[] charArray)

Since: API Level 1

Prints the string representation of the specified character array to the target stream.

Parameters
charArray the character array to print to the target stream.
See Also

public void print (long lnum)

Since: API Level 1

Prints the string representation of the specified long to the target stream.

Parameters
lnum the long value to print to the target stream.
See Also

public void print (int inum)

Since: API Level 1

Prints the string representation of the specified integer to the target stream.

Parameters
inum the integer value to print to the target stream.
See Also

public void print (boolean bool)

Since: API Level 1

Prints the string representation of the specified boolean to the target stream.

Parameters
bool the boolean value to print the target stream.
See Also

public PrintStream printf (Locale l, String format, Object... args)

Since: API Level 1

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 if l is null.
format the format string (see format(String, Object...))
args the list of arguments passed to the formatter. If there are more arguments than required by format, 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 if format == null.

public PrintStream printf (String format, Object... args)

Since: API Level 1

Prints a formatted string. The behavior of this method is the same as this stream's #format(String, Object...) method. For the locale, the default value of the current virtual machine instance is used.

Parameters
format the format string (see format(String, Object...))
args the list of arguments passed to the formatter. If there are more arguments than required by format, 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 if format == null

public void println ()

Since: API Level 1

Prints the string representation of the system property "line.separator" to the target stream.

public void println (float fnum)

Since: API Level 1

Prints the string representation of the specified float followed by the system property "line.separator" to the target stream.

Parameters
fnum the float value to print to the target stream.
See Also

public void println (int inum)

Since: API Level 1

Prints the string representation of the specified integer followed by the system property "line.separator" to the target stream.

Parameters
inum the integer value to print to the target stream.
See Also

public void println (long lnum)

Since: API Level 1

Prints the string representation of the specified long followed by the system property "line.separator" to the target stream.

Parameters
lnum the long value to print to the target stream.
See Also

public void println (Object obj)

Since: API Level 1

Prints the string representation of the specified object followed by the system property "line.separator" to the target stream.

Parameters
obj the object to print to the target stream.
See Also

public void println (char[] charArray)

Since: API Level 1

Prints the string representation of the specified character array followed by the system property "line.separator" to the target stream.

Parameters
charArray the character array to print to the target stream.
See Also

public synchronized void println (String str)

Since: API Level 1

Prints a string followed by the system property "line.separator" 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 with write(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

public void println (char ch)

Since: API Level 1

Prints the string representation of the specified character followed by the system property "line.separator" to the target stream.

Parameters
ch the character to print to the target stream.
See Also

public void println (double dnum)

Since: API Level 1

Prints the string representation of the specified double followed by the system property "line.separator" to the target stream.

Parameters
dnum the double value to print to the target stream.
See Also

public void println (boolean bool)

Since: API Level 1

Prints the string representation of the specified boolean followed by the system property "line.separator" to the target stream.

Parameters
bool the boolean value to print to the target stream.
See Also

public synchronized void write (int oneByte)

Since: API Level 1

Writes one byte to the target stream. Only the least significant byte of the integer 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.

Parameters
oneByte the byte to be written

public void write (byte[] buffer, int offset, int length)

Since: API Level 1

Writes 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.

Parameters
buffer 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.
Throws
IndexOutOfBoundsException if offset < 0 or count < 0, or if offset + count is bigger than the length of buffer.
See Also

Protected Methods

protected void clearError ()

Since: API Level 9

Sets the error state of the stream to false.

protected void setError ()

Since: API Level 1

Sets the error flag of this print stream to true.