PrintWriter
open class PrintWriter : Writer
kotlin.Any | ||
↳ | java.io.Writer | |
↳ | java.io.PrintWriter |
Prints formatted representations of objects to a text-output stream. This class implements all of the print
methods found in PrintStream
. It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.
Unlike the PrintStream
class, if automatic flushing is enabled it will be done only when one of the println
, printf
, or format
methods is invoked, rather than whenever a newline character happens to be output. These methods use the platform's own notion of line separator rather than the newline character.
Methods in this class never throw I/O exceptions, although some of its constructors may. The client may inquire as to whether any errors have occurred by invoking checkError()
.
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 | |
---|---|
PrintWriter(out: Writer) Creates a new PrintWriter, without automatic line flushing. |
|
PrintWriter(out: Writer, autoFlush: Boolean) Creates a new PrintWriter. |
|
PrintWriter(out: OutputStream) Creates a new PrintWriter, without automatic line flushing, from an existing OutputStream. |
|
PrintWriter(out: OutputStream, autoFlush: Boolean) Creates a new PrintWriter from an existing OutputStream. |
|
PrintWriter(out: OutputStream, autoFlush: Boolean, charset: Charset) Creates a new PrintWriter from an existing OutputStream. |
|
PrintWriter(fileName: String) Creates a new PrintWriter, without automatic line flushing, with the specified file name. |
|
PrintWriter(fileName: String, csn: String) Creates a new PrintWriter, without automatic line flushing, with the specified file name and charset. |
|
PrintWriter(fileName: String, charset: Charset) Creates a new PrintWriter, without automatic line flushing, with the specified file name and charset. |
|
PrintWriter(file: File) Creates a new PrintWriter, without automatic line flushing, with the specified file. |
|
PrintWriter(file: File, csn: String) Creates a new PrintWriter, without automatic line flushing, with the specified file and charset. |
|
PrintWriter(file: File, charset: Charset) Creates a new PrintWriter, without automatic line flushing, with the specified file and charset. |
Public methods | |
---|---|
open PrintWriter |
append(csq: CharSequence?) Appends the specified character sequence to this writer. |
open PrintWriter |
append(csq: CharSequence?, start: Int, end: Int) Appends a subsequence of the specified character sequence to this writer. |
open PrintWriter |
Appends the specified character to this writer. |
open Boolean |
Flushes the stream if it's not closed and checks its error state. |
open Unit |
close() Closes the stream and releases any system resources associated with it. |
open Unit |
flush() Flushes the stream. |
open PrintWriter |
Writes a formatted string to this writer using the specified format string and arguments. |
open PrintWriter |
Writes a formatted string to this writer using the specified format string and arguments. |
open Unit |
Prints a boolean value. |
open Unit |
Prints a character. |
open Unit |
Prints an integer. |
open Unit |
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 |
Prints an object. |
open PrintWriter |
A convenience method to write a formatted string to this writer using the specified format string and arguments. |
open PrintWriter |
A convenience method to write a formatted string to this writer using the specified format string and arguments. |
open Unit |
println() Terminates the current line by writing the line separator string. |
open Unit |
Prints a boolean value and then terminates the line. |
open Unit |
Prints a character and then terminates the line. |
open Unit |
Prints an integer and then terminates the line. |
open Unit |
Prints a long integer and then terminates the line. |
open Unit |
Prints a floating-point number and then terminates the line. |
open Unit |
Prints a double-precision floating-point number and then terminates the line. |
open Unit |
Prints an array of characters and then terminates the line. |
open Unit |
Prints a String and then terminates the line. |
open Unit |
Prints an Object and then terminates the line. |
open Unit |
Writes a single character. |
open Unit |
Writes A Portion of an array of characters. |
open Unit |
Writes an array of characters. |
open Unit |
Writes a portion of a string. |
open Unit |
Writes a string. |
Protected methods | |
---|---|
open Unit |
Clears the error state of this stream. |
open Unit |
setError() Indicates that an error has occurred. |
Inherited functions | |
---|---|
Properties | |
---|---|
Writer! |
The underlying character-output stream of this |
Inherited properties | |
---|---|
Public constructors
PrintWriter
PrintWriter(out: Writer)
Creates a new PrintWriter, without automatic line flushing.
Parameters | |
---|---|
out |
Writer: A character-output stream |
PrintWriter
PrintWriter(
out: Writer,
autoFlush: Boolean)
Creates a new PrintWriter.
Parameters | |
---|---|
out |
Writer: A character-output stream |
autoFlush |
Boolean: A boolean; if true, the println, printf, or format methods will flush the output buffer |
PrintWriter
PrintWriter(out: OutputStream)
Creates a new PrintWriter, without automatic line flushing, from an existing OutputStream. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will convert characters into bytes using the default character encoding.
Parameters | |
---|---|
out |
OutputStream: An output stream |
PrintWriter
PrintWriter(
out: OutputStream,
autoFlush: Boolean)
Creates a new PrintWriter from an existing OutputStream. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will convert characters into bytes using the default character encoding.
Parameters | |
---|---|
out |
OutputStream: An output stream |
autoFlush |
Boolean: A boolean; if true, the println , printf , or format methods will flush the output buffer |
PrintWriter
PrintWriter(
out: OutputStream,
autoFlush: Boolean,
charset: Charset)
Creates a new PrintWriter from an existing OutputStream. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will convert characters into bytes using the specified charset.
Parameters | |
---|---|
out |
OutputStream: An output stream |
autoFlush |
Boolean: A boolean; if true, the println , printf , or format methods will flush the output buffer |
charset |
Charset: A charset |
PrintWriter
PrintWriter(fileName: String)
Creates a new PrintWriter, 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 writer. 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 string 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 |
PrintWriter
PrintWriter(
fileName: String,
csn: String)
Creates a new PrintWriter, 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 writer. 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 string 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 |
PrintWriter
PrintWriter(
fileName: String,
charset: Charset)
Creates a new PrintWriter, 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 writer. 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 |
PrintWriter
PrintWriter(file: File)
Creates a new PrintWriter, 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 writer. 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 |
PrintWriter
PrintWriter(
file: File,
csn: String)
Creates a new PrintWriter, 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 writer. 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 |
PrintWriter
PrintWriter(
file: File,
charset: Charset)
Creates a new PrintWriter, 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 writer. 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
open fun append(csq: CharSequence?): PrintWriter
Appends the specified character sequence to this writer.
An invocation of this method of the form out.append(csq)
behaves in exactly the same way as the invocation
<code>out.write(csq.toString()) </code>
Depending on the specification of toString
for the character sequence csq
, the entire sequence may not be appended. For instance, invoking the 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 writer. |
Return | |
---|---|
PrintWriter |
This writer |
Exceptions | |
---|---|
java.io.IOException |
If an I/O error occurs |
append
open fun append(
csq: CharSequence?,
start: Int,
end: Int
): PrintWriter
Appends a subsequence of the specified character sequence to this writer.
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.write(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 | |
---|---|
PrintWriter |
This writer |
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
open fun append(c: Char): PrintWriter
Appends the specified character to this writer.
An invocation of this method of the form out.append(c)
behaves in exactly the same way as the invocation
<code>out.write(c) </code>
Parameters | |
---|---|
c |
Char: The 16-bit character to append |
Return | |
---|---|
PrintWriter |
This writer |
Exceptions | |
---|---|
java.io.IOException |
If an I/O error occurs |
checkError
open fun checkError(): Boolean
Flushes the stream if it's not closed and checks its error state.
Return | |
---|---|
Boolean |
true if the print stream has encountered an error, either on the underlying output stream or during a format conversion. |
close
open fun close(): Unit
Closes the stream and releases any system resources associated with it. Closing a previously closed stream has no effect.
Exceptions | |
---|---|
java.lang.Exception |
if this resource cannot be closed |
java.io.IOException |
If an I/O error occurs |
See Also
flush
open fun flush(): Unit
Flushes the stream.
Exceptions | |
---|---|
java.io.IOException |
If an I/O error occurs |
See Also
format
open fun format(
format: String,
vararg args: Any!
): PrintWriter
Writes a formatted string to this writer using the specified format string and arguments. If automatic flushing is enabled, calls to this method will flush the output buffer.
The locale always used is the one returned by Locale.getDefault()
, 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 | |
---|---|
PrintWriter |
This writer |
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
open fun format(
l: Locale?,
format: String,
vararg args: Any!
): PrintWriter
Writes a formatted string to this writer using the specified format string and arguments. If automatic flushing is enabled, calls to this method will flush the output buffer.
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 | |
---|---|
PrintWriter |
This writer |
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 |
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 |
open fun print(c: Char): Unit
Prints a character. The character is translated into one or more bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int)
method.
Parameters | |
---|---|
c |
Char: The char to be printed |
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 |
See Also
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 |
See Also
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 |
See Also
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 |
See Also
open fun print(s: CharArray!): Unit
Prints an array of characters. The characters are converted 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 | |
---|---|
s |
CharArray!: The array of chars to be printed |
Exceptions | |
---|---|
java.lang.NullPointerException |
If s is null |
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 platform's default character encoding, and these bytes are written in exactly the manner of the write(int)
method.
Parameters | |
---|---|
s |
String?: The String to be printed |
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 |
See Also
printf
open fun printf(
format: String,
vararg args: Any!
): PrintWriter
A convenience method to write a formatted string to this writer using the specified format string and arguments. If automatic flushing is enabled, calls to this method will flush the output buffer.
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 | |
---|---|
PrintWriter |
This writer |
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
open fun printf(
l: Locale?,
format: String,
vararg args: Any!
): PrintWriter
A convenience method to write a formatted string to this writer using the specified format string and arguments. If automatic flushing is enabled, calls to this method will flush the output buffer.
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 | |
---|---|
PrintWriter |
This writer |
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
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
open fun println(x: Boolean): Unit
Prints a boolean value and then terminates the line. This method behaves as though it invokes print(boolean)
and then println()
.
Parameters | |
---|---|
x |
Boolean: the boolean value to be printed |
println
open fun println(x: Char): Unit
Prints a character and then terminates the line. This method behaves as though it invokes print(char)
and then println()
.
Parameters | |
---|---|
x |
Char: the char value to be printed |
println
open fun println(x: Int): Unit
Prints an integer and then terminates the line. This method behaves as though it invokes print(int)
and then println()
.
Parameters | |
---|---|
x |
Int: the int value to be printed |
println
open fun println(x: Long): Unit
Prints a long integer and then terminates the line. This method behaves as though it invokes print(long)
and then println()
.
Parameters | |
---|---|
x |
Long: the long value to be printed |
println
open fun println(x: Float): Unit
Prints a floating-point number and then terminates the line. This method behaves as though it invokes print(float)
and then println()
.
Parameters | |
---|---|
x |
Float: the float value to be printed |
println
open fun println(x: Double): Unit
Prints a double-precision floating-point number and then terminates the line. This method behaves as though it invokes print(double)
and then println()
.
Parameters | |
---|---|
x |
Double: the double value to be printed |
println
open fun println(x: CharArray!): Unit
Prints an array of characters and then terminates the line. This method behaves as though it invokes print(char[])
and then println()
.
Parameters | |
---|---|
x |
CharArray!: the array of char values to be printed |
println
open fun println(x: String?): Unit
Prints a String and then terminates the line. This method behaves as though it invokes print(java.lang.String)
and then println()
.
Parameters | |
---|---|
x |
String?: the String value to be printed |
println
open fun println(x: Any?): Unit
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 print(java.lang.String)
and then println()
.
Parameters | |
---|---|
x |
Any?: The Object to be printed. |
write
open fun write(c: Int): Unit
Writes a single character.
Parameters | |
---|---|
c |
Int: int specifying a character to be written. |
Exceptions | |
---|---|
java.io.IOException |
If an I/O error occurs |
write
open fun write(
buf: CharArray!,
off: Int,
len: Int
): Unit
Writes A Portion of an array of characters.
Parameters | |
---|---|
cbuf |
Array of characters |
off |
Int: Offset from which to start writing characters |
len |
Int: Number of characters to write |
buf |
CharArray!: Array of characters |
Exceptions | |
---|---|
java.lang.IndexOutOfBoundsException |
If the values of the off and len parameters cause the corresponding method of the underlying Writer to throw an IndexOutOfBoundsException |
java.io.IOException |
If an I/O error occurs |
write
open fun write(buf: CharArray!): Unit
Writes an array of characters. This method cannot be inherited from the Writer class because it must suppress I/O exceptions.
Parameters | |
---|---|
cbuf |
Array of characters to be written |
buf |
CharArray!: Array of characters to be written |
Exceptions | |
---|---|
java.io.IOException |
If an I/O error occurs |
write
open fun write(
s: String,
off: Int,
len: Int
): Unit
Writes a portion of a string.
Parameters | |
---|---|
str |
A String |
off |
Int: Offset from which to start writing characters |
len |
Int: Number of characters to write |
s |
String: A String |
Exceptions | |
---|---|
java.lang.IndexOutOfBoundsException |
If the values of the off and len parameters cause the corresponding method of the underlying Writer to throw an IndexOutOfBoundsException |
java.io.IOException |
If an I/O error occurs |
write
open fun write(s: String): Unit
Writes a string. This method cannot be inherited from the Writer class because it must suppress I/O exceptions.
Parameters | |
---|---|
str |
String to be written |
s |
String: String to be written |
Exceptions | |
---|---|
java.io.IOException |
If an I/O error occurs |
Protected methods
clearError
protected open fun clearError(): Unit
Clears the 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
protected open fun setError(): Unit
Indicates that an error has occurred.
This method will cause subsequent invocations of checkError()
to return true
until clearError()
is invoked.
Properties
out
protected var out: Writer!
The underlying character-output stream of this PrintWriter
.