Classes
class ConsoleReader
public class ConsoleReader <: InputStream
Description: Reads data from the console and converts the data into characters or strings.
This class cannot be used to construct instances. Instances can be obtained only by using the getStdIn() function.
The read operation is synchronous. A buffer is set to store the content input from the console. At the end of the input stream of the console, the read function of the console returns None.
NOTE
ConsoleReader has only one instance, and all methods share the same buffer. The
readmethod returnsNonein the following scenarios:
- When a standard input stream is redirected to a file, the end of file (EOF) is read.
- On Linux,
Ctrl+Dis pressed.- On Windows, Enter is pressed after
Ctrl+Zis pressed.
Parent types:
func read()
public func read(): ?Rune
Description: Reads the next character from a standard input stream.
Returns:
Throws:
- IllegalArgumentException: If a string that does not comply with the
UTF-8encoding format is input, this exception is thrown.
Examples:
import std.env.*
main() {
getStdOut().write("Input information:")
var c = getStdIn().read() // Input: abc
var r = c.getOrThrow()
getStdOut().write("The input information is:")
getStdOut().writeln(r)
return
}
Results:
Input information: abc
The input information is: a
func read(Array<Byte>)
public func read(arr: Array<Byte>): Int64
Description: Reads data from a standard input stream and saves the data to 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
Examples:
import std.env.*
main() {
var arr = Array<Byte>(3, repeat: 0)
getStdOut().write("Input information:")
getStdIn().read (arr) // Input: 567
getStdOut().write("The input array is:")
getStdOut().writeln(arr)
return
}
Results:
Input information: 567
The input array is: 567
func readToEnd()
public func readToEnd(): ?String
Description: Reads all characters from a standard input stream.
The reading continues until the EOF is read, or Ctrl+D is entered on Linux or Ctrl+Z and Enter are pressed on Windows. If all characters are read, ?String is returned. Otherwise, None is returned. If the reading fails, None is returned. This API does not throw any exception. Even if the input is not a UTF-8 string, a string instance is constructed and returned. The behavior is equal to 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:
Examples:
import std.env.*
main() {
getStdOut().write("Input information:")
var c = getStdIn().readUntil({ch: Rune => ch > r'd' && ch < r'g'}) // Input: abcdefg
var r = c.getOrThrow()
getStdOut().writeln("The input information is: " + r)
return
}
Results:
Input information: abcdefg
The input information is: abcde
func readUntil(Rune)
public func readUntil(ch: Rune): ?String
Description: Reads data from a standard input stream until characters ch are read.
Characters ch are contained in the result. If the EOF is read, all the read information is returned. If the reading fails, None is returned.
Parameters:
- ch: Rune: termination characters
Returns:
Examples:
import std.env.*
main() {
getStdOut().write("Input information:")
var c = getStdIn().readUntil(r'b') // Input: abc
var r = c.getOrThrow()
getStdOut().write("The input information is:")
getStdOut().writeln(r)
return
}
Results:
Input information: abc
The input information is: ab
func readln()
public func readln(): ?String
Description: Reads a row of strings from a standard input stream.
If characters are read, ?String is returned. The result does not contain newline characters at the end. This API does not throw any exception. Even if the input is not a UTF-8 string, a string instance is constructed and returned. The behavior is equal to String.fromUtf8Uncheck(Array<Byte>).
Returns:
- ?String: read row data. If the reading fails,
Noneis returned.
Examples:
import std.env.*
main() {
getStdOut().write("Input information:")
var c = getStdIn().readln() // Input: abc
var r = c.getOrThrow()
getStdOut().write("The input information is:")
getStdOut().writeln(r)
return
}
Results:
Input information: abc
The input information is: abc
class ConsoleWriter
public class ConsoleWriter <: OutputStream
Description: Provides the standard output functionality 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 class cannot be used to construct instances. You can only obtain standard output instances by calling getStdOut() or standard error instances by calling getStdErr().
Parent types:
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 buffer to a standard output stream or a 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 stream or a 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 stream or a 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 stream or a 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 stream or a 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 stream or a 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 stream or a 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 stream or a 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 stream or a standard error stream.
Parameters:
- v: Int8: value to be written
func write(Rune)
public func write(v: Rune): Unit
Description: Writes a specified Unicode character value of Rune to a standard output stream or a standard error stream.
Parameters:
- v: Rune: value to be written
func write(String)
public func write(v: String): Unit
Description: Writes the specified string value to a standard output stream or a 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 stream or a 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 stream or a 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 stream or a 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 stream or a 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 stream or a 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 buffer (followed by a newline character) to a standard output stream or a 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 stream or a 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 stream or a 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 stream or a 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 stream or a 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 stream or a 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 stream or a 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 stream or a 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 stream or a 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 stream or a 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 stream or a 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 stream or a 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 stream or a 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 stream or a 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 stream or a 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 stream or a standard error stream.
Parameters:
- v: T: value to be written