Tabular IQ

TextReader Class

Class Index By Category

Overview

The TextReader class represents a file access stream for reading text values from a file.

Methods

TextReader.close
Closes the input text file
TextReader.readLine
Read a single line from a text file

Example

// the file this example reads, in the system's temporary directory
// (Environment.getTempPath() does not include a trailing separator)
const path = `${Environment.getTempPath()}/testfile.txt`;

// write a small file first, so this example runs on its own
const writer = File.createText(path);
writer.writeLine("Line 1");
writer.writeLine("Line 2");
writer.writeLine("Line 3");
writer.close();

// try to open the text file
const reader = File.openText(path);
if (!reader) {
    alert("Can't open the file.  Does it exist?");
} else {
    // file opened successfully;
    // read in all of the lines in the file

    let text = "";
    let line;
    while ((line = reader.readLine()) !== null) {
        text += `${line}\n`;
    }

    reader.close();

    alert(text);
}

TextReader.close

function TextReader.close() : Boolean

Returns

True if the file was successfully closed, false if the file was not open when close() was invoked.

Description

Closes the input text file. All subsequent calls to readLine() will return null.

TextReader.readLine

function TextReader.readLine() : String

Returns

A string containing the line read from the input text file. If the file is not open, the method returns a null value.

Description

Reads a single line from a text file. The file pointer is automatically advanced to the next line. A string containing the line read is returned, and does not include the carriage return or line-feed character. If the file is not open, a null value is returned.