import ballerina/io;
import ballerina/log;
function process(io:ReadableCharacterChannel sc,
                 io:WritableCharacterChannel dc) returns error? {
    string intermediateCharacterString = " my name is ";
    string greetingText = check sc.read(5);
    string name = check sc.read(15);
    var writeCharResult = check dc.write(greetingText, 0);
    var writeCharResult1 = check dc.write(intermediateCharacterString, 0);
    var writeCharResult2 = check dc.write(name, 1);
    return;
}
function closeRc(io:ReadableCharacterChannel ch) {
    var cr = ch.close();
    if (cr is error) {
        log:printError("Error occurred while closing the channel: ", err = cr);
    }
}
function closeWc(io:WritableCharacterChannel ch) {
    var cr = ch.close();
    if (cr is error) {
        log:printError("Error occurred while closing the channel: ", err = cr);
    }
}public function main() returns error? {
    io:ReadableByteChannel readableFieldResult = check io:openReadableFile("./files/sample.txt");
    io:ReadableCharacterChannel sourceChannel =
            new(readableFieldResult, "UTF-8");
    io:WritableByteChannel writableFileResult = check io:openWritableFile("./files/sampleResponse.txt");
    io:WritableCharacterChannel destinationChannel =
            new(writableFileResult, "UTF-8");
    io:println("Started to process the file.");
    var result = process(sourceChannel, destinationChannel);
    if (result is error) {
        log:printError("error occurred while processing chars ", err = result);
    } else {
        io:println("File processing complete.");
    }
    closeRc(sourceChannel);
    closeWc(destinationChannel);
}

Character I/O

This demonstrates how characters could be read and written through I/O APIs.

import ballerina/io;
import ballerina/log;
function process(io:ReadableCharacterChannel sc,
                 io:WritableCharacterChannel dc) returns error? {
    string intermediateCharacterString = " my name is ";

This function reads content from a file, appends the additional string, and writes the content.

    string greetingText = check sc.read(5);
    string name = check sc.read(15);

Reads the characters from the source channel.

    var writeCharResult = check dc.write(greetingText, 0);
    var writeCharResult1 = check dc.write(intermediateCharacterString, 0);
    var writeCharResult2 = check dc.write(name, 1);
    return;
}

Writes the characters to the destination channel.

function closeRc(io:ReadableCharacterChannel ch) {
    var cr = ch.close();
    if (cr is error) {
        log:printError("Error occurred while closing the channel: ", err = cr);
    }
}

Closes the readable character channel.

function closeWc(io:WritableCharacterChannel ch) {
    var cr = ch.close();
    if (cr is error) {
        log:printError("Error occurred while closing the channel: ", err = cr);
    }
}

Closes the writable character channel.

public function main() returns error? {
    io:ReadableByteChannel readableFieldResult = check io:openReadableFile("./files/sample.txt");
    io:ReadableCharacterChannel sourceChannel =
            new(readableFieldResult, "UTF-8");
    io:WritableByteChannel writableFileResult = check io:openWritableFile("./files/sampleResponse.txt");
    io:WritableCharacterChannel destinationChannel =
            new(writableFileResult, "UTF-8");
    io:println("Started to process the file.");
    var result = process(sourceChannel, destinationChannel);
    if (result is error) {
        log:printError("error occurred while processing chars ", err = result);
    } else {
        io:println("File processing complete.");
    }

Processes the given string.

    closeRc(sourceChannel);

Closes the readable channel.

    closeWc(destinationChannel);
}

Closes the writable channel.

# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command.
$ ballerina run character_io.bal
Started to process the file.
File processing complete.