Functions

createReadableChannel
I

Creates an in-memory channel, which will be a reference stream of bytes.

fileReadBlocksAsStream
I

Read the entire file content as a stream of blocks.

fileReadBytes
I

Read the entire file content as a byte array.

fileReadCsv
I

Read file content as a CSV.

fileReadCsvAsStream
I

Read file content as a CSV.

fileReadJson
I

Reads file content as a JSON.

fileReadLines
I

Reads the entire file content as a list of lines.

fileReadLinesAsStream
I

Reads file content as a stream of lines.

fileReadString
I

Reads the entire file content as a string.

fileReadXml
I

Reads file content as an XML.

fileWriteBlocksFromStream
I

Write a byte stream to a file.

fileWriteBytes
I

Write a set of bytes to a file.

fileWriteCsv
I

Write CSV content to a file.

fileWriteCsvFromStream
I

Write CSV record stream to a file.

fileWriteJson
I

Write a JSON to a file.

fileWriteLines
I

Write an array of lines to a file.

fileWriteLinesFromStream
I

Write stream of lines to a file.

fileWriteString
I

Write a string content to a file.

fileWriteXml
I

Write XML content to a file.

openReadableCsvFile
I

Retrieves a readable CSV channel from a given file path.

openReadableFile
I

Retrieves a ReadableByteChannel from a given file path.

openWritableCsvFile
I

Retrieves a writable CSV channel from a given file path.

openWritableFile
I

Retrieves a WritableByteChannel from a given file path.

print
I

Prints any, error, or string templates(such as The respective int value is ${val}) value(s) to the STDOUT.

println
I

Prints any, error or string templates(such as The respective int value is ${val}) value(s) to the STDOUT followed by a new line.

readln

Retrieves the input read from the STDIN.

createReadableChannel

function createReadableChannel(byte[ ] content) returns ReadableByteChannel | Error
Isolated Function

Creates an in-memory channel, which will be a reference stream of bytes.

1var byteChannel = io:createReadableChannel(content);

Parameters

  • content byte[ ]
  • Content, which should be exposed as a channel

Return Type

(ReadableByteChannel | Error)

The io:ReadableByteChannel related to the given bytes or else an io:Error if any error occurred

fileReadBlocksAsStream

function fileReadBlocksAsStream(string path, int blockSize) returns stream<Block, Error?> | Error
Isolated Function

Read the entire file content as a stream of blocks.

1stream<io:Block, io:Error?>|io:Error content = io:fileReadBlocksAsStream("./resources/myfile.txt", 1000);

Parameters

  • path string
  • The path of the file

  • blockSize int (default 4096)
  • An optional size of the byte block. The default size is 4KB

Return Type

(stream<Block, Error?> | Error)

A byte block stream or anio:Error

fileReadBytes

function fileReadBytes(string path) returns readonly & byte[ ] | Error
Isolated Function

Read the entire file content as a byte array.

1byte[]|io:Error content = io:fileReadBytes("./resources/myfile.txt");

Parameters

  • path string
  • The path of the file

Return Type

(readonly & byte[ ] | Error)

A read-only byte array or an io:Error

fileReadCsv

function fileReadCsv(string path, int skipHeaders) returns string[ ][ ] | Error
Isolated Function

Read file content as a CSV.

1string[][]|io:Error content = io:fileReadCsv("./resources/myfile.csv");

Parameters

  • skipHeaders int (default 0)
  • Number of headers, which should be skipped prior to reading records

Return Type

(string[ ][ ] | Error)

The entire CSV content in the channel as an array of string arrays or an io:Error

fileReadCsvAsStream

function fileReadCsvAsStream(string path) returns stream<string[ ], Error?> | Error
Isolated Function

Read file content as a CSV.

1stream<string[], io:Error?>|io:Error content = io:fileReadCsvAsStream("./resources/myfile.csv");

Parameters

Return Type

(stream<string[ ], Error?> | Error)

The entire CSV content in the channel a stream of string arrays or an io:Error

fileReadJson

function fileReadJson(string path) returns json | Error
Isolated Function

Reads file content as a JSON.

1json|io:Error content = io:fileReadJson("./resources/myfile.json");

Parameters

  • path string
  • The path of the JSON file

Return Type

(json | Error)

The file content as a JSON object or an io:Error

fileReadLines

function fileReadLines(string path) returns string[ ] | Error
Isolated Function

Reads the entire file content as a list of lines. The resulting string array does not contain the terminal carriage (e.g., \r or \n).

1string[]|io:Error content = io:fileReadLines("./resources/myfile.txt");

Parameters

  • path string
  • The path of the file

Return Type

(string[ ] | Error)

The file as list of lines or an io:Error

fileReadLinesAsStream

function fileReadLinesAsStream(string path) returns stream<string, Error?> | Error
Isolated Function

Reads file content as a stream of lines. The resulting stream does not contain the terminal carriage (e.g., \r or \n).

1stream<string, io:Error?>|io:Error content = io:fileReadLinesAsStream("./resources/myfile.txt");

Parameters

  • path string
  • The path of the file

Return Type

(stream<string, Error?> | Error)

The file content as a stream of strings or an io:Error

fileReadString

function fileReadString(string path) returns string | Error
Isolated Function

Reads the entire file content as a string. The resulting string output does not contain the terminal carriage (e.g., \r or \n).

1string|io:Error content = io:fileReadString("./resources/myfile.txt");

Parameters

  • path string
  • The path of the file

Return Type

(string | Error)

The entire file content as a string or an io:Error

fileReadXml

function fileReadXml(string path) returns xml | Error
Isolated Function

Reads file content as an XML.

1xml|io:Error content = io:fileReadXml("./resources/myfile.xml");

Parameters

  • path string
  • The path of the XML file

Return Type

(xml | Error)

The file content as an XML or an io:Error

fileWriteBlocksFromStream

function fileWriteBlocksFromStream(string path, stream<byte[ ], Error?> byteStream, FileWriteOption option) returns Error?
Isolated Function

Write a byte stream to a file.

1byte[] content = [[60, 78, 39, 28]];
2stream<byte[], io:Error?> byteStream = content.toStream();
3io:Error? result = io:fileWriteBlocksFromStream("./resources/myfile.txt", byteStream);

Parameters

  • path string
  • The path of the file

  • byteStream stream<byte[ ], Error?>
  • Byte stream to write

  • option FileWriteOption (default OVERWRITE)
  • To indicate whether to overwrite or append the given content

Return Type

(Error?)

An io:Error or else ()

fileWriteBytes

function fileWriteBytes(string path, byte[ ] content, FileWriteOption option) returns Error?
Isolated Function

Write a set of bytes to a file.

1byte[] content = [60, 78, 39, 28];
2io:Error? result = io:fileWriteBytes("./resources/myfile.txt", content);

Parameters

  • path string
  • The path of the file

  • content byte[ ]
  • Byte content to write

  • option FileWriteOption (default OVERWRITE)
  • To indicate whether to overwrite or append the given content

Return Type

(Error?)

An io:Error or else ()

fileWriteCsv

function fileWriteCsv(string path, string[ ][ ] content, FileWriteOption option) returns Error?
Isolated Function

Write CSV content to a file.

1string[][] content = [["Anne", "Johnson", "SE"], ["John", "Cameron", "QA"]];
2io:Error? result = io:fileWriteCsv("./resources/myfile.csv", content);

Parameters

  • content string[ ][ ]
  • CSV content as an array of string arrays

  • option FileWriteOption (default OVERWRITE)
  • To indicate whether to overwrite or append the given content

Return Type

(Error?)

An io:Error or () when the writing was successful

fileWriteCsvFromStream

function fileWriteCsvFromStream(string path, stream<string[ ], Error?> content, FileWriteOption option) returns Error?
Isolated Function

Write CSV record stream to a file.

1string[][] content = [["Anne", "Johnson", "SE"], ["John", "Cameron", "QA"]];
2stream<string[], io:Error?> recordStream = content.toStream();
3io:Error? result = io:fileWriteCsvFromStream("./resources/myfile.csv", recordStream);

Parameters

  • option FileWriteOption (default OVERWRITE)
  • To indicate whether to overwrite or append the given content

Return Type

(Error?)

An io:Error or () when the writing was successful

fileWriteJson

function fileWriteJson(string path, json content) returns Error?
Isolated Function

Write a JSON to a file.

1json content = {"name": "Anne", "age": 30};
2io:Error? content = io:fileWriteJson("./resources/myfile.json");

Parameters

  • path string
  • The path of the JSON file

  • content json
  • JSON content to write

Return Type

(Error?)

() when the writing was successful or an io:Error

fileWriteLines

function fileWriteLines(string path, string[ ] content, FileWriteOption option) returns Error?
Isolated Function

Write an array of lines to a file. During the writing operation, a newline character \n will be added after each line.

1string[] content = ["Hello Universe..!!", "How are you?"];
2io:Error? result = io:fileWriteLines("./resources/myfile.txt", content);

Parameters

  • path string
  • The path of the file

  • content string[ ]
  • An array of string lines to write

  • option FileWriteOption (default OVERWRITE)
  • To indicate whether to overwrite or append the given content

Return Type

(Error?)

() when the writing was successful or an io:Error

fileWriteLinesFromStream

function fileWriteLinesFromStream(string path, stream<string, Error?> lineStream, FileWriteOption option) returns Error?
Isolated Function

Write stream of lines to a file. During the writing operation, a newline character \n will be added after each line.

1string content = ["Hello Universe..!!", "How are you?"];
2stream<string, io:Error?> lineStream = content.toStream();
3io:Error? result = io:fileWriteLinesFromStream("./resources/myfile.txt", lineStream);

Parameters

  • path string
  • The path of the file

  • option FileWriteOption (default OVERWRITE)
  • To indicate whether to overwrite or append the given content

Return Type

(Error?)

() when the writing was successful or an io:Error

fileWriteString

function fileWriteString(string path, string content, FileWriteOption option) returns Error?
Isolated Function

Write a string content to a file.

1string content = "Hello Universe..!!";
2io:Error? result = io:fileWriteString("./resources/myfile.txt", content);

Parameters

  • path string
  • The path of the file

  • content string
  • String content to write

  • option FileWriteOption (default OVERWRITE)
  • To indicate whether to overwrite or append the given content

Return Type

(Error?)

() when the writing was successful or an io:Error

fileWriteXml

function fileWriteXml(string path, xml content, FileWriteOption fileWriteOption, *XmlWriteOptions xmlOptions) returns Error?
Isolated Function

Write XML content to a file.

1xml content = xml `<book>The Lost World</book>`;
2io:Error? result = io:fileWriteXml("./resources/myfile.xml", content);

Parameters

  • path string
  • The path of the XML file

  • content xml
  • XML content to write

  • fileWriteOption FileWriteOption (default OVERWRITE)
  • file write option(OVERWRITE and APPEND are the possible values, and the default value is OVERWRITE)

  • xmlOptions *XmlWriteOptions
  • XML writing options(XML entity type and DOCTYPE)

Return Type

(Error?)

() value when the writing was successful or an io:Error

openReadableCsvFile

function openReadableCsvFile(string path, Separator fieldSeparator, string charset, int skipHeaders) returns ReadableCSVChannel | Error
Isolated Function

Retrieves a readable CSV channel from a given file path.

1io:ReadableCSVChannel rCsvChannel = check io:openReadableCsvFile(srcFileName);

Parameters

  • path string
  • File path, which describes the location of the CSV

  • fieldSeparator Separator (default ",")
  • CSV record separator (i.e., comma or tab)

  • charset string (default "UTF-8")
  • Representation of the encoding characters in the file

  • skipHeaders int (default 0)
  • Number of headers, which should be skipped

Return Type

(ReadableCSVChannel | Error)

The io:ReadableCSVChannel, which could be used to iterate through the CSV records or else an io:Error if any error occurred

openReadableFile

function openReadableFile(string path) returns ReadableByteChannel | Error
Isolated Function

Retrieves a ReadableByteChannel from a given file path.

1io:ReadableByteChannel readableFieldResult = check io:openReadableFile("./files/sample.txt");

Parameters

  • path string
  • Relative/absolute path string to locate the file

Return Type

(ReadableByteChannel | Error)

The io:ReadableByteChannel related to the given file or else an io:Error if there is an error while opening

openWritableCsvFile

function openWritableCsvFile(string path, Separator fieldSeparator, string charset, int skipHeaders, FileWriteOption option) returns WritableCSVChannel | Error
Isolated Function

Retrieves a writable CSV channel from a given file path.

1io:WritableCSVChannel wCsvChannel = check io:openWritableCsvFile(srcFileName);

Parameters

  • path string
  • File path, which describes the location of the CSV

  • fieldSeparator Separator (default ",")
  • CSV record separator (i.e., comma or tab)

  • charset string (default "UTF-8")
  • Representation of the encoding characters in the file

  • skipHeaders int (default 0)
  • Number of headers, which should be skipped

  • option FileWriteOption (default OVERWRITE)
  • To indicate whether to overwrite or append the given content

Return Type

(WritableCSVChannel | Error)

The WritableCSVChannel, which could be used to write the CSV records or else an io:Error if any error occurred

openWritableFile

function openWritableFile(string path, FileWriteOption option) returns WritableByteChannel | Error
Isolated Function

Retrieves a WritableByteChannel from a given file path.

1io:WritableByteChannel writableFileResult = check io:openWritableFile("./files/sampleResponse.txt");

Parameters

  • path string
  • Relative/absolute path string to locate the file

  • option FileWriteOption (default OVERWRITE)
  • To indicate whether to overwrite or append the given content

Return Type

(WritableByteChannel | Error)

The io:WritableByteChannel related to the given file or else an io:Error if any error occurred

print

function print(Printable... values)
Isolated Function

Prints any, error, or string templates(such as The respective int value is ${val}) value(s) to the STDOUT.

1io:print("Start processing the CSV file from ", srcFileName);

Parameters

  • values Printable...
  • The value(s) to be printed

println

function println(Printable... values)
Isolated Function

Prints any, error or string templates(such as The respective int value is ${val}) value(s) to the STDOUT followed by a new line.

1io:println("Start processing the CSV file from ", srcFileName);

Parameters

  • values Printable...
  • The value(s) to be printed

readln

function readln(any a) returns string

Retrieves the input read from the STDIN.

1string choice = io:readln("Enter choice 1 - 5: ");

Parameters

  • a any
  • Any value to be printed

Return Type

(string)

Input read from the STDIN