The TextReader class represents a file access stream for reading text values from a file.
// 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);
} True if the file was successfully closed, false if the file was not open when close() was invoked.
Closes the input text file. All subsequent calls to readLine() will return null.
A string containing the line read from the input text file. If the file is not open, the method returns a null value.
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.