I/O module

This module contains the required functions and types to stream handling. The distinction is made between reading from a stream (streamreader) and writing to a stream (streamwriter). You can implement your own stream types as soon as they respect the following contracts.

Streams take an optional charset argument. The charset is used to correctly decode or encode characters, strings or other values into bytes. By default this module will use the default charset returned by the function charset.default().

Note

To use the features of this module, you have to put a special import statement at the begining of your LSP file: use io;

Functions

io.openRead(filename)
io.openRead(filename, charset)

Opens file filename in reading mode. This function returns a streamreader or throws an exception if the file cannot be opened. If the charset is omitted, this function will use the default charset returned by charset.default().

Parameters:
  • filename (string) – Path to the file to open.
  • charset (charset) – Encoding used to convert bytes to characters.
Return type:

streamreader

io.openWrite(filename)
io.openWrite(filename, charset)

Opens file filename in writing mode. If the file already exists, it is truncated first. This function returns a streamwriter or throws an exception if the file cannot be opened. If the charset is omitted, this function will use the default charset returned by charset.default().

Parameters:
  • filename (string) – Path to the file to open.
  • charset (charset) – Encoding used to convert bytes to characters.
Return type:

streamwriter

io.openAppend(filename)
io.openAppend(filename, charset)

Opens file filename in appending mode. This function returns a streamwriter or throws an exception if the file cannot be opened. If the charset is omitted, this function will use the default charset returned by charset.default().

Parameters:
  • filename (string) – Path to the file to open.
  • charset (charset) – Encoding used to convert bytes to characters.
Return type:

streamwriter

Attributes

This module defines the following attributes:

io.stdin

Standard input stream. This attribute is readonly.

io.stdout

Standard output stream. This attribute is readonly.

io.stderr

Standard error stream. This attribute is readonly.

Types

type streamreader

Default type for input streams.

close()

Closes the current stream. If the stream is already closed, do nothing. Note that one once no more value refers to a stream, it is automatically closed.

eof()

Returns true if the end of file is reached.

read(n)

Reads ands returns up to n characters in the stream as a string. The returned string can have less than n characters if the end of the stream is reached before.

Return type:string
readInt()

Reads and returns the next integer available in the stream. After calling this function, the stream cursor is positioned at the next non whitespace character or at the end of the stream if the remaining characters contains only whitespaces (see string.isWhitespace() for the definition of a whitespace). If the read sequence cannot be converted to a valid integer, an error is thrown. If the stream is already positioned at the end-of-file or is not readable, an error is thrown.

Return type:int
readDouble()

Reads and returns the next floating-point number available in the stream. After calling this function, the stream cursor is positioned at the next non whitespace character or at the end of the stream if the remaining characters contains only whitespaces (see string.isWhitespace() for the definition of a whitespace). If the read sequence cannot be converted to a valid integer, an error is thrown. If the stream is already positioned at the end-of-file or is not readable, an error is thrown.

Return type:double
readString()

Reads and returns the next sequence of non whitespace characters. After calling this function, the stream cursor is positioned at the start of the next string or at the end of the stream if the remaining characters contains only whitespaces (see string.isWhitespace() for the definition of a whitespace). If the file is already positioned at the end-of-file or is not readable, an error is thrown.

Return type:string
readln()

Reads ands returns the next line in the stream. After calling this function, the stream cursor is positioned at the start of the next line or at the end of the stream if no characters are available. If the stream is not readable or if no line is available, an error is thrown. Note that the line terminator is trimmed and is not returned.

Return type:string
type streamwriter

Default type for output streams.

close()

Closes the current stream. If the stream is already closed, do nothing. Note that one once no more value refers to a stream, it is automatically closed.

flush()

Flush the stream. Useful for buffered streams.

print([arg0[, arg1[, ...]]])

Writes the arguments in the stream. The values will be converted to strings. If an argument is not convertible to a string, an error is thrown. Arguments are written without space or special formatting.

println([arg0[, arg1[, ...]]])

Same as print() but adds a new line in the stream.