Class: ReadableCSVChannel

Represents a ReadableCSVChannel which could be used to read records from CSV file.

Constructor

Constructs a CSV channel from a CharacterChannel to read/write CSV records.

init (ReadableCharacterChannel byteChannel, Separator fs, int nHeaders)
  • fs Separator ","
  • Field separator, which will separate between the records in the CSV file

  • nHeaders int 0
  • Number of headers, which should be skipped prior to reading records

Methods

skipHeaders

Skips the given number of headers.

hasNext

Indicates whether there's another record, which could be read.

getNext

Gets the next record from the CSV file.

csvStream

Returns a CSV record stream that can be used to CSV records as a stream.

close

Closes a given CSVChannel.

getTable

Returns a table, which corresponds to the CSV records.

skipHeaders

function skipHeaders(int nHeaders)
Isolated Function

Skips the given number of headers.

1readableCSVChannel.skipHeaders(5);

Parameters

  • nHeaders int
  • The number of headers, which should be skipped

hasNext

function hasNext() returns boolean
Isolated Function

Indicates whether there's another record, which could be read.

1boolean hasNext = readableCSVChannel.hasNext();

Return Type

(boolean)

True if there's a record

getNext

function getNext() returns string[ ] | Error?
Isolated Function

Gets the next record from the CSV file.

1string[]|io:Error? record = readableCSVChannel.getNext();

Return Type

(string[ ] | Error?)

List of fields in the CSV or else an io:Error

csvStream

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

Returns a CSV record stream that can be used to CSV records as a stream.

1stream<string[], io:Error>|io:Error? record = readableCSVChannel.csvStream();

Return Type

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

Either a stream of records(string[]) or else an io:Error

close

function close() returns Error?
Isolated Function

Closes a given CSVChannel. After a channel is closed, any further reading operations will cause an error.

1io:Error? err = readableCSVChannel.close();

Return Type

(Error?)

io:Error if any error occurred

getTable

function getTable(typedesc<record { }> structType, string[ ] fieldNames) returns table<record { }> | Error
Isolated Function

Returns a table, which corresponds to the CSV records.

1var tblResult1 = readableCSVChannel.getTable(Employee);
2var tblResult2 = readableCSVChannel.getTable(Employee, ["id", "name"]);

Parameters

  • structType typedesc<record { }>
  • The object in which the CSV records should be deserialized

  • fieldNames string[ ] (default [])
  • The names of the fields used as the (composite)key of the table

Return Type

(table<record { }> | Error)

Table, which represents the CSV records or else an io:Error