Class
class Console
public class Console
Description: Provides APIs for obtaining standard input, standard output, and standard error streams.
static prop stdErr
public static prop stdErr: ConsoleWriter
Description: This member attribute is of the ConsoleWriter type and provides the function of obtaining standard error streams.
Type: ConsoleWriter
static prop stdIn
public static prop stdIn: ConsoleReader
Description: This member attribute is of the ConsoleReader type and provides the function of obtaining standard input streams.
Type: ConsoleReader
static prop stdOut
public static prop stdOut: ConsoleWriter
Description: This member attribute is of the ConsoleWriter type and provides the function of obtaining standard output streams.
Type: ConsoleWriter
class ConsoleReader
public class ConsoleReader <: InputStream
Description: Reads data from the console and converts the data into characters or strings.
This type does not support constructing instances. Its instances can be obtained only through Console.stdIn.
The read operation is synchronous. There is a buffer in ConsoleReader used to store the content input from the console. When the end of the input stream of the console is reached, the read function of the console returns None
.
Note:
ConsoleReader has only one instance, and all methods share the same buffer. The
read
method returnsNone
in the following scenarios:
- When a standard input stream is redirected to a file, the end-of-file (EOF) mark is read.
- On Linux,
Ctrl+D
is pressed.- On Windows, Enter is pressed after
Ctrl+Z
is pressed.The ConsoleReader supports only the UTF-8 encoding. In the Windows environment, you need to manually run the
chcp 65001
command. (Change encoding of Windows terminal to UTF-8) If the system code is inconsistent with the code required by the API, garbled characters may be generated. As a result, an exception occurs when the system code is read from the'Rune` and IllegalArgumentException is thrown.
Parent Type:
func read()
public func read(): ?Rune
Description: Reads the next character from a standard input stream.
Returns:
- ?Rune: ?Rune is returned if a character is read. Otherwise,
None
is returned.
Throws:
- IllegalArgumentException: If a string that does not comply with the
UTF-8
encoding format is input, this exception is thrown.
func read(Array<Byte>)
public func read(arr: Array<Byte>): Int64
Description: Reads data from a standard input stream and puts the data into arr
.
Note:
This function has risks. The read result may truncate
UTF-8 code point
. If truncation occurs, the result of converting the Array<Byte> instance into a string is incorrect or an exception is thrown.
Parameters:
Returns:
- Int64: length of the read bytes
func readToEnd()
public func readToEnd(): ?String
Description: Reads all characters from a standard input stream.
The reading continues until the EOF
mark is read, or Ctrl+D
is entered on Linux or Ctrl+Z
+ Enter is pressed on Windows. If all characters are read, ?String is returned. Otherwise, None
is returned. If the read operation fails, None
is returned. This API does not throw any exception. Even if a string that does not comply with the UTF-8
encoding format is entered, a String instance is constructed and returned. The behavior is the same as String.fromUtf8Uncheck(Array<Byte>).
Returns:
func readUntil((Rune) -> Bool)
public func readUntil(predicate: (Rune) -> Bool): ?String
Description: Reads data from a standard input stream until the read characters meet the condition of predicate
.
The characters that meet the condition of predicate: (Rune) -> Bool are contained in the result. If the reading fails, None
is returned.
Parameters:
- predicate: (Rune) ->Bool: condition for stopping reading
Returns:
func readUntil(Rune)
public func readUntil(ch: Rune): ?String
Description: Reads data from a standard input stream until the character ch
is read.
The character ch
is contained in the result. If the EOF mark is read, all the read information is returned. If the reading fails, None
is returned.
Parameters:
- ch: Rune: end character
Returns:
func readln()
public func readln(): ?String
Description: Reads a line of characters from a standard input stream.
If characters are read, a ?String instance is returned. The result does not contain newline characters. This API does not throw any exception. Even if a string that does not comply with the UTF-8
encoding format is entered, a String instance is constructed and returned. The behavior is the same as String.fromUtf8Uncheck(Array<Byte>).
Returns:
- ?String: read line data. If the reading fails,
None
is returned.
class ConsoleWriter
public class ConsoleWriter <: OutputStream
Description: Provides the standard output function that ensures thread security.
The result written to the console is complete each time the write function is called. The results of different write function calls are not mixed. This type does not support constructing instances. Standard output instances can be obtained only through Console.stdOut and standard error instances can be obtained only through Console.stdErr.
Parent Type:
func flush()
public func flush(): Unit
Description: Refreshes the output stream.
func write(Array<Byte>)
public func write(buffer: Array<Byte>): Unit
Description: Writes the byte array specified by buffer to a standard output or standard error stream.
Parameters:
func write(Bool)
public func write(v: Bool): Unit
Description: Writes the text representation of a specified Boolean value to a standard output or standard error stream.
Parameters:
- v: Bool: value to be written
func write(Float16)
public func write(v: Float16): Unit
Description: Writes the text representation of a specified 16-bit floating-point number value to a standard output or standard error stream.
Parameters:
- v: Float16: value to be written
func write(Float32)
public func write(v: Float32): Unit
Description: Writes the text representation of a specified 32-bit floating-point number value to a standard output or standard error stream.
Parameters:
- v: Float32: value to be written
func write(Float64)
public func write(v: Float64): Unit
Description: Writes the text representation of a specified 64-bit floating-point number value to a standard output or standard error stream.
Parameters:
- v: Float64: value to be written
func write(Int16)
public func write(v: Int16): Unit
Description: Writes the text representation of a specified 16-bit signed integer value to a standard output or standard error stream.
Parameters:
- v: Int16: value to be written
func write(Int32)
public func write(v: Int32): Unit
Description: Writes the text representation of a specified 32-bit signed integer value to a standard output or standard error stream.
Parameters:
- v: Int32: value to be written
func write(Int64)
public func write(v: Int64): Unit
Description: Writes the text representation of a specified 64-bit signed integer value to a standard output or standard error stream.
Parameters:
- v: Int64: value to be written
func write(Int8)
public func write(v: Int8): Unit
Description: Writes the text representation of a specified 8-bit signed integer value to a standard output or standard error stream.
Parameters:
- v: Int8: value to be written
func write(Rune)
public func write(v: Rune): Unit
Description: Writes the Unicode character value of a specified Rune to a standard output or standard error stream.
Parameters:
- v: Rune: value to be written
func write(String)
public func write(v: String): Unit
Description: Writes a specified string value to a standard output or standard error stream.
Parameters:
- v: String: value to be written
func write(UInt16)
public func write(v: UInt16): Unit
Description: Writes the text representation of a specified 16-bit unsigned integer value to a standard output or standard error stream.
Parameters:
- v: UInt16: value to be written
func write(UInt32)
public func write(v: UInt32): Unit
Description: Writes the text representation of a specified 32-bit unsigned integer value to a standard output or standard error stream.
Parameters:
- v: UInt32: value to be written
func write(UInt64)
public func write(v: UInt64): Unit
Description: Writes the text representation of a specified 64-bit unsigned integer value to a standard output or standard error stream.
Parameters:
- v: UInt64: value to be written
func write(UInt8)
public func write(v: UInt8): Unit
Description: Writes the text representation of a specified 8-bit unsigned integer value to a standard output or standard error stream.
Parameters:
- v: UInt8: value to be written
func write<T>(T) where T <: ToString
public func write<T>(v: T): Unit where T <: ToString
Description: Writes the data type that implements the ToString interface to a standard output or standard error stream.
Parameters:
- v: T: instance of the ToString type to be written
func writeln(Array<Byte>)
public func writeln(buffer: Array<Byte>): Unit
Description: Writes the byte array specified by buffer (followed by a newline character) to a standard output or standard error stream.
Parameters:
func writeln(Bool)
public func writeln(v: Bool): Unit
Description: Writes the text representation (followed by a newline character) of a specified Boolean value to a standard output or standard error stream.
Parameters:
- v: Bool: value to be written
func writeln(Float16)
public func writeln(v: Float16): Unit
Description: Writes the text representation (followed by a newline character) of a specified 16-bit floating-point number value to a standard output or standard error stream.
Parameters:
- v: Float16: value to be written
func writeln(Float32)
public func writeln(v: Float32): Unit
Description: Writes the text representation (followed by a newline character) of a specified 32-bit floating-point number value to a standard output or standard error stream.
Parameters:
- v: Float32: value to be written
func writeln(Float64)
public func writeln(v: Float64): Unit
Description: Writes the text representation (followed by a newline character) of a specified 64-bit floating-point number value to a standard output or standard error stream.
Parameters:
- v: Float64: value to be written
func writeln(Int16)
public func writeln(v: Int16): Unit
Description: Writes the text representation (followed by a newline character) of a specified 16-bit signed integer value to a standard output or standard error stream.
Parameters:
- v: Int16: value to be written
func writeln(Int32)
public func writeln(v: Int32): Unit
Description: Writes the text representation (followed by a newline character) of a specified 32-bit signed integer value to a standard output or standard error stream.
Parameters:
- v: Int32: value to be written
func writeln(Int64)
public func writeln(v: Int64): Unit
Description: Writes the text representation (followed by a newline character) of a specified 64-bit signed integer value to a standard output or standard error stream.
Parameters:
- v: Int64: value to be written
func writeln(Int8)
public func writeln(v: Int8): Unit
Description: Writes the text representation (followed by a newline character) of a specified 8-bit signed integer value to a standard output or standard error stream.
Parameters:
- v: Int8: value to be written
func writeln(Rune)
public func writeln(v: Rune): Unit
Description: Writes the specified Unicode character value (followed by a newline character) to a standard output or standard error stream.
Parameters:
- v: Rune: value to be written
func writeln(String)
public func writeln(v: String): Unit
Description: Writes the specified string value (followed by a newline character) to a standard output or standard error stream.
Parameters:
- v: String: value to be written
func writeln(UInt16)
public func writeln(v: UInt16): Unit
Description: Writes the text representation (followed by a newline character) of a specified 16-bit unsigned integer value to a standard output or standard error stream.
Parameters:
- v: UInt16: value to be written
func writeln(UInt32)
public func writeln(v: UInt32): Unit
Description: Writes the text representation (followed by a newline character) of a specified 32-bit unsigned integer value to a standard output or standard error stream. Parameters:
- v: UInt32: value to be written
func writeln(UInt64)
public func writeln(v: UInt64): Unit
Description: Writes the text representation (followed by a newline character) of a specified 64-bit unsigned integer value to a standard output or standard error stream.
Parameters:
- v: UInt64: value to be written
func writeln(UInt8)
public func writeln(v: UInt8): Unit
Description: Writes the text representation (followed by a newline character) of a specified 8-bit unsigned integer value to a standard output or standard error stream.
Parameters:
- v: UInt8: value to be written
func writeln<T>(T) where T <: ToString
public func writeln<T>(v: T): Unit where T <: ToString
Description: Writes the string (followed by a newline character) converted from a data type that implements the ToString interface to a standard output or standard error stream.
Parameters:
- v: T: value to be written