Added in API level 1

PrintStream

open class PrintStream : FilterOutputStream, Appendable, Closeable
kotlin.Any
   ↳ java.io.OutputStream
   ↳ java.io.FilterOutputStream
   ↳ java.io.PrintStream

A 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 platform's default character encoding 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.

Summary

Public constructors

Creates a new print stream, without automatic line flushing, with the specified OutputStream.

PrintStream(out: OutputStream!, autoFlush: Boolean)

Creates a new print stream, with the specified OutputStream and line flushing.

PrintStream(out: OutputStream!, autoFlush: Boolean, encoding: String!)

Creates a new print stream, with the specified OutputStream, line flushing, and character encoding.

PrintStream(out: OutputStream!, autoFlush: Boolean, charset: Charset!)

Creates a new print stream, with the specified OutputStream, line flushing and charset.

PrintStream(fileName: String!)

Creates a new print stream, without automatic line flushing, with the specified file name.

PrintStream(fileName: String!, csn: String!)

Creates a new print stream, without automatic line flushing, with the specified file name and charset.

PrintStream(fileName: String!, charset: Charset!)

Creates a new print stream, without automatic line flushing, with the specified file name and charset.

PrintStream(file: File!)

Creates a new print stream, without automatic line flushing, with the specified file.

PrintStream(file: File!, csn: String!)

Creates a new print stream, without automatic line flushing, with the specified file and charset.

PrintStream(file: File!, charset: Charset!)

Creates a new print stream, without automatic line flushing, with the specified file and charset.

Public methods
open PrintStream

Appends the specified character sequence to this output stream.

open PrintStream
append(csq: CharSequence?, start: Int, end: Int)

Appends a subsequence of the specified character sequence to this output stream.

open PrintStream

Appends the specified character to this output stream.

open Boolean

Flushes the stream and checks its error state.

open Unit

Closes the stream.

open Unit

Flushes the stream.

open PrintStream!
format(format: String!, vararg args: Any!)

Writes a formatted string to this output stream using the specified format string and arguments.

open PrintStream!
format(l: Locale!, format: String!, vararg args: Any!)

Writes a formatted string to this output stream using the specified format string and arguments.

open Unit

Prints a boolean value.

open Unit
print(c: Char)

Prints a character.

open Unit
print(i: Int)

Prints an integer.

open Unit
print(l: Long)

Prints a long integer.

open Unit

Prints a floating-point number.

open Unit

Prints a double-precision floating-point number.

open Unit

Prints an array of characters.

open Unit

Prints a string.

open Unit
print(obj: Any!)

Prints an object.

open PrintStream!
printf(format: String!, vararg args: Any!)

A convenience method to write a formatted string to this output stream using the specified format string and arguments.

open PrintStream!
printf(l: Locale!, format: String!, vararg args: Any!)

A convenience method to write a formatted string to this output stream using the specified format string and arguments.

open Unit

Terminates the current line by writing the line separator string.

open Unit

Prints a boolean and then terminate the line.

open Unit

Prints a character and then terminate the line.

open Unit

Prints an integer and then terminate the line.

open Unit

Prints a long and then terminate the line.

open Unit

Prints a float and then terminate the line.

open Unit

Prints a double and then terminate the line.

open Unit

Prints an array of characters and then terminate the line.

open Unit

Prints a String and then terminate the line.

open Unit
println(x: Any!)

Prints an Object and then terminate the line.

open Unit
write(b: Int)

Writes the specified byte to this stream.

open Unit
write(buf: ByteArray!, off: Int, len: Int)

Writes len bytes from the specified byte array starting at offset off to this stream.

open Unit

Writes all bytes from the specified byte array to this stream.

open Unit

Writes all bytes from the specified byte array to this stream.

Protected methods
open Unit

Clears the internal error state of this stream.

open Unit

Sets the error state of the stream to true.

Inherited functions
Inherited properties

Public constructors

PrintStream

Added in API level 1
PrintStream(out: OutputStream!)

Creates a new print stream, without automatic line flushing, with the specified OutputStream. Characters written to the stream are converted to bytes using the platform's default character encoding.

Parameters
out OutputStream!: The output stream to which values and objects will be printed

PrintStream

Added in API level 1
PrintStream(
    out: OutputStream!,
    autoFlush: Boolean)

Creates a new print stream, with the specified OutputStream and line flushing. Characters written to the stream are converted to bytes using the platform's default character encoding.

Parameters
out OutputStream!: The output stream to which values and objects will be printed
autoFlush Boolean: Whether 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 written

PrintStream

Added in API level 1
PrintStream(
    out: OutputStream!,
    autoFlush: Boolean,
    encoding: String!)

Creates a new print stream, with the specified OutputStream, line flushing, and character encoding.

Parameters
out OutputStream!: The output stream to which values and objects will be printed
autoFlush Boolean: Whether 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 written
encoding String!: The name of a supported character encoding
Exceptions
java.io.UnsupportedEncodingException If the named encoding is not supported

PrintStream

Added in API level 1
PrintStream(
    out: OutputStream!,
    autoFlush: Boolean,
    charset: Charset!)

Creates a new print stream, with the specified OutputStream, line flushing and charset. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the provided charset.

Parameters
out OutputStream!: The output stream to which values and objects will be printed
autoFlush Boolean: Whether 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 written
charset Charset!: A charset

PrintStream

Added in API level 1
PrintStream(fileName: String!)

Creates a new print stream, without automatic line flushing, with the specified file name. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the default charset for this instance of the Java virtual machine.

Parameters
fileName 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.
Exceptions
java.io.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 file
java.lang.SecurityException If a security manager is present and java.lang.SecurityManager#checkWrite denies write access to the file

PrintStream

Added in API level 1
PrintStream(
    fileName: String!,
    csn: String!)

Creates a new print stream, without automatic line flushing, with the specified file name and charset. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the provided charset.

Parameters
fileName 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.
csn String!: The name of a supported charset
Exceptions
java.io.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 file
java.lang.SecurityException If a security manager is present and java.lang.SecurityManager#checkWrite denies write access to the file
java.io.UnsupportedEncodingException If the named charset is not supported

PrintStream

Added in API level 1
PrintStream(
    fileName: String!,
    charset: Charset!)

Creates a new print stream, without automatic line flushing, with the specified file name and charset. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the provided charset.

Parameters
fileName 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.
charset Charset!: A charset
Exceptions
java.io.IOException if an I/O error occurs while opening or creating the file
java.lang.SecurityException If a security manager is present and java.lang.SecurityManager#checkWrite denies write access to the file

PrintStream

Added in API level 1
PrintStream(file: File!)

Creates a new print stream, without automatic line flushing, with the specified file. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the default charset for this instance of the Java virtual machine.

Parameters
file 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.
Exceptions
java.io.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 file
java.lang.SecurityException If a security manager is present and java.lang.SecurityManager#checkWrite denies write access to the file

PrintStream

Added in API level 1
PrintStream(
    file: File!,
    csn: String!)

Creates a new print stream, without automatic line flushing, with the specified file and charset. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the provided charset.

Parameters
file 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 String!: The name of a supported charset
Exceptions
java.io.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 file
java.lang.SecurityException If a security manager is present and java.lang.SecurityManager#checkWrite denies write access to the file
java.io.UnsupportedEncodingException If the named charset is not supported

PrintStream

Added in API level 1
PrintStream(
    file: File!,
    charset: Charset!)

Creates a new print stream, without automatic line flushing, with the specified file and charset. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the provided charset.

Parameters
file 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 Charset!: A charset
Exceptions
java.io.IOException if an I/O error occurs while opening or creating the file
java.lang.SecurityException If a security manager is present and java.lang.SecurityManager#checkWrite denies write access to the file

Public methods

append

Added in API level 1
open fun append(csq: CharSequence?): PrintStream

Appends the specified character sequence to this output stream.

An invocation of this method of the form out.append(csq) behaves in exactly the same way as the invocation

<code>out.print(csq.toString())
  </code>

Depending on the specification of toString for the character sequence csq, the entire sequence may not be appended. For instance, invoking then toString method of a character buffer will return a subsequence whose content depends upon the buffer's position and limit.

Parameters
csq CharSequence?: The character sequence to append. If csq is null, then the four characters "null" are appended to this output stream.
Return
PrintStream This output stream
Exceptions
java.io.IOException If an I/O error occurs

append

Added in API level 1
open fun append(
    csq: CharSequence?,
    start: Int,
    end: Int
): PrintStream

Appends a subsequence of the specified character sequence to this output stream.

An invocation of this method of the form out.append(csq, start, end) when csq is not null, behaves in exactly the same way as the invocation

<code>out.print(csq.subSequence(start, end).toString())
  </code>
Parameters
csq CharSequence?: The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters "null".
start Int: The index of the first character in the subsequence
end Int: The index of the character following the last character in the subsequence
Return
PrintStream This output stream
Exceptions
java.lang.IndexOutOfBoundsException If start or end are negative, start is greater than end, or end is greater than csq.length()
java.io.IOException If an I/O error occurs

append

Added in API level 1
open fun append(c: Char): PrintStream

Appends the specified character to this output stream.

An invocation of this method of the form out.append(c) behaves in exactly the same way as the invocation

<code>out.print(c)
  </code>
Parameters
c Char: The 16-bit character to append
Return
PrintStream This output stream
Exceptions
java.io.IOException If an I/O error occurs

checkError

Added in API level 1
open fun checkError(): Boolean

Flushes the stream and checks its error state. The internal error state is set to true when the underlying output stream throws an IOException other than InterruptedIOException, and when the setError method is invoked. If an operation on the underlying output stream throws an InterruptedIOException, then the PrintStream converts the exception back into an interrupt by doing:

<code>Thread.currentThread().interrupt();
  </code>
or the equivalent.

Return
Boolean true if and only if this stream has encountered an IOException other than InterruptedIOException, or the setError method has been invoked

close

Added in API level 1
open fun close(): Unit

Closes the stream. This is done by flushing the stream and then closing the underlying output stream.

Exceptions
java.lang.Exception if this resource cannot be closed
java.io.IOException if an I/O error occurs

See Also

    flush

    Added in API level 1
    open fun flush(): Unit

    Flushes the stream. This is done by writing any buffered output bytes to the underlying output stream and then flushing that stream.

    Exceptions
    java.io.IOException if an I/O error occurs.

    format

    Added in API level 1
    open fun format(
        format: String!,
        vararg args: Any!
    ): PrintStream!

    Writes a formatted string to this output stream using the specified format string and arguments.

    The locale always used is the one returned by java.util.Locale#getDefault(Locale.Category) with FORMAT category specified, regardless of any previous invocations of other formatting methods on this object.

    Parameters
    format String!: A format string as described in Format string syntax
    args Any!: 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 null argument depends on the conversion.
    Return
    PrintStream! This output stream
    Exceptions
    java.util.IllegalFormatException If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.
    java.lang.NullPointerException If the format is null

    format

    Added in API level 1
    open fun format(
        l: Locale!,
        format: String!,
        vararg args: Any!
    ): PrintStream!

    Writes a formatted string to this output stream using the specified format string and arguments.

    Parameters
    l Locale!: The locale to apply during formatting. If l is null then no localization is applied.
    format String!: A format string as described in Format string syntax
    args Any!: 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 null argument depends on the conversion.
    Return
    PrintStream! This output stream
    Exceptions
    java.util.IllegalFormatException If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.
    java.lang.NullPointerException If the format is null

    print

    Added in API level 1
    open fun print(b: Boolean): Unit

    Prints a boolean value. The string produced by java.lang.String#valueOf(boolean) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the #write(int) method.

    Parameters
    b Boolean: The boolean to be printed
    Added in API level 1
    open fun print(c: Char): Unit

    Prints a character. The character is translated into one or more bytes according to the character encoding given to the constructor, or the platform's default character encoding if none specified. These bytes are written in exactly the manner of the #write(int) method.

    Parameters
    c Char: The char to be printed
    Added in API level 1
    open fun print(i: Int): Unit

    Prints an integer. The string produced by java.lang.String#valueOf(int) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the #write(int) method.

    Parameters
    i Int: The int to be printed
    Added in API level 1
    open fun print(l: Long): Unit

    Prints a long integer. The string produced by java.lang.String#valueOf(long) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the #write(int) method.

    Parameters
    l Long: The long to be printed
    Added in API level 1
    open fun print(f: Float): Unit

    Prints a floating-point number. The string produced by java.lang.String#valueOf(float) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the #write(int) method.

    Parameters
    f Float: The float to be printed
    Added in API level 1
    open fun print(d: Double): Unit

    Prints a double-precision floating-point number. The string produced by java.lang.String#valueOf(double) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the #write(int) method.

    Parameters
    d Double: The double to be printed
    Added in API level 1
    open fun print(s: CharArray!): Unit

    Prints an array of characters. The characters are converted into bytes according to the character encoding given to the constructor, or the platform's default character encoding if none specified. These bytes are written in exactly the manner of the #write(int) method.

    Parameters
    s CharArray!: The array of chars to be printed
    Exceptions
    java.lang.NullPointerException If s is null
    Added in API level 1
    open fun print(s: String!): Unit

    Prints a string. If the argument is null then the string "null" is printed. Otherwise, the string's characters are converted into bytes according to the character encoding given to the constructor, or the platform's default character encoding if none specified. These bytes are written in exactly the manner of the #write(int) method.

    Parameters
    s String!: The String to be printed
    Added in API level 1
    open fun print(obj: Any!): Unit

    Prints an object. The string produced by the java.lang.String#valueOf(Object) method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the #write(int) method.

    Parameters
    obj Any!: The Object to be printed

    printf

    Added in API level 1
    open fun printf(
        format: String!,
        vararg args: Any!
    ): PrintStream!

    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.printf(format, args) behaves in exactly the same way as the invocation

    <code>out.format(format, args)
      </code>
    Parameters
    format String!: A format string as described in Format string syntax
    args Any!: 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 null argument depends on the conversion.
    Return
    PrintStream! This output stream
    Exceptions
    java.util.IllegalFormatException If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.
    java.lang.NullPointerException If the format is null

    printf

    Added in API level 1
    open fun printf(
        l: Locale!,
        format: String!,
        vararg args: Any!
    ): PrintStream!

    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.printf(l, format, args) behaves in exactly the same way as the invocation

    <code>out.format(l, format, args)
      </code>
    Parameters
    l Locale!: The locale to apply during formatting. If l is null then no localization is applied.
    format String!: A format string as described in Format string syntax
    args Any!: 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 null argument depends on the conversion.
    Return
    PrintStream! This output stream
    Exceptions
    java.util.IllegalFormatException If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.
    java.lang.NullPointerException If the format is null

    println

    Added in API level 1
    open fun println(): Unit

    Terminates the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n').

    println

    Added in API level 1
    open fun println(x: Boolean): Unit

    Prints a boolean and then terminate the line. This method behaves as though it invokes print(boolean) and then println().

    Parameters
    x Boolean: The boolean to be printed

    println

    Added in API level 1
    open fun println(x: Char): Unit

    Prints a character and then terminate the line. This method behaves as though it invokes print(char) and then println().

    Parameters
    x Char: The char to be printed.

    println

    Added in API level 1
    open fun println(x: Int): Unit

    Prints an integer and then terminate the line. This method behaves as though it invokes print(int) and then println().

    Parameters
    x Int: The int to be printed.

    println

    Added in API level 1
    open fun println(x: Long): Unit

    Prints a long and then terminate the line. This method behaves as though it invokes print(long) and then println().

    Parameters
    x Long: a The long to be printed.

    println

    Added in API level 1
    open fun println(x: Float): Unit

    Prints a float and then terminate the line. This method behaves as though it invokes print(float) and then println().

    Parameters
    x Float: The float to be printed.

    println

    Added in API level 1
    open fun println(x: Double): Unit

    Prints a double and then terminate the line. This method behaves as though it invokes print(double) and then println().

    Parameters
    x Double: The double to be printed.

    println

    Added in API level 1
    open fun println(x: CharArray!): Unit

    Prints an array of characters and then terminate the line. This method behaves as though it invokes print(char[]) and then println().

    Parameters
    x CharArray!: an array of chars to print.

    println

    Added in API level 1
    open fun println(x: String!): Unit

    Prints a String and then terminate the line. This method behaves as though it invokes print(java.lang.String) and then println().

    Parameters
    x String!: The String to be printed.

    println

    Added in API level 1
    open fun println(x: Any!): Unit

    Prints an Object and then terminate the line. This method calls at first String.valueOf(x) to get the printed object's string value, then behaves as though it invokes print(java.lang.String) and then println().

    Parameters
    x Any!: The Object to be printed.

    write

    Added in API level 1
    open fun write(b: Int): Unit

    Writes the specified byte to this stream. If the byte is a newline and automatic flushing is enabled then the flush method will be invoked on the underlying output stream.

    Note that the byte is written as given; to write a character that will be translated according to the platform's default character encoding, use the print(char) or println(char) methods.

    Parameters
    b Int: The byte to be written
    Exceptions
    java.io.IOException if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.

    write

    Added in API level 1
    open fun write(
        buf: ByteArray!,
        off: Int,
        len: Int
    ): Unit

    Writes len bytes from the specified byte array starting at offset off to this stream. If automatic flushing is enabled then the flush method will be invoked on the underlying output stream.

    Note that the bytes will be written as given; to write characters that will be translated according to the platform's default character encoding, use the print(char) or println(char) methods.

    Parameters
    b the data.
    off Int: Offset from which to start taking bytes
    len Int: Number of bytes to write
    buf ByteArray!: A byte array
    Exceptions
    java.io.IOException if an I/O error occurs. In particular, an IOException is thrown if the output stream is closed.

    write

    Added in API level 1
    open fun write(buf: ByteArray!): Unit

    Writes all bytes from the specified byte array to this stream. If automatic flushing is enabled then the flush method will be invoked on the underlying output stream.

    Note that the bytes will be written as given; to write characters that will be translated according to the platform's default character encoding, use the print(char[]) or println(char[]) methods.

    Parameters
    b the data to be written.
    buf ByteArray!: A byte array
    Exceptions
    java.io.IOException If an I/O error occurs.

    writeBytes

    Added in API level 34
    open fun writeBytes(buf: ByteArray!): Unit

    Writes all bytes from the specified byte array to this stream. If automatic flushing is enabled then the flush method will be invoked.

    Note that the bytes will be written as given; to write characters that will be translated according to the platform's default character encoding, use the print(char[]) or println(char[]) methods.

    Parameters
    buf ByteArray!: A byte array

    Protected methods

    clearError

    Added in API level 9
    protected open fun clearError(): Unit

    Clears the internal error state of this stream.

    This method will cause subsequent invocations of checkError() to return false until another write operation fails and invokes setError().

    setError

    Added in API level 1
    protected open fun setError(): Unit

    Sets the error state of the stream to true.

    This method will cause subsequent invocations of checkError() to return true until clearError() is invoked.