Skip to content

Core\IO::readText

Core\IO::readText(string $path, {charset?: Core\Charset}): stringCore\IO::readText(
    string $path,
    {charset?: Core\Charset},
): string

Reads the whole content of a text file that may be saved in another character set.

A character set says how the letters of a text are stored as bytes. Older programs often save files as Latin-1 or Windows-1252, and some Windows programs save them as UTF-16.

Core\IO::readText takes a path and a charset option with a Core\Charset case. It reads the file and converts it to a normal Novis string. Without the option, the file is read as UTF-8.

The conversion is exact. When a byte in the file is not valid in that character set, readText throws a RuntimeError. The message gives the position of that byte. No character is replaced or left out.

readText throws an IOError when the file does not exist or cannot be read.

A program you start from the command line may read any path. On a server, a program may read only the files in its application folder, except nvs.toml and names that start with .. Any other path needs the fs.read capability. Without it, readText throws a RuntimeError.

Parameters

  • $pathstringThe file to read. A relative path must be a string written directly in the code. It is joined to the folder of the file that contains it.

Options

  • charsetCore\CharsetoptionalThe encoding of the file's bytes. It is Core\Charset::Utf8 when it is not given. Novis strings are UTF-8, so UTF-8 text is read unchanged.

Return value

Returns string. The file's content as a string, converted from $charset. It never contains a replacement character, because a conversion that cannot be exact throws an error.

Errors

  • RuntimeErrorThe configuration does not grant fs.read for this path. Or the file's bytes are not valid $charset. In that case the message names the offset of the first invalid sequence.
  • IOErrorThe capability allowed the call, but the operating system did not. The file does not exist, is a directory, or could not be read.

Changelog

VersionChange
0.0.5When a request is recorded, each call to Core\IO::read, Core\IO::readText, Core\IO::readBytes, Core\IO::write and Core\IO::append is saved with its line and its file path. The editor shows the number of these calls and their time above the line.

Examples

Read a latin 1 file

<?nvs
// Shows how to read a file that was saved in the Latin-1 character set.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'city.txt');
// `encodeText` gives the Latin-1 bytes of the text. In Latin-1, "ö" is one byte.
array<bytes> $parts = [Core\Encoding::encodeText("Köln\n", Core\Charset::Latin1)];
Core\IO::writeStream($path, $parts, { overwrite: true });
// The `charset` option says how the file was saved. This prints "Köln".
echo Core\IO::readText($path, { charset: Core\Charset::Latin1 });
// Without the option, `readText` reads UTF-8. These bytes are not UTF-8, so it throws an error.
try {
Core\IO::readText($path);
} catch (RuntimeError $e) {
echo "the file is not UTF-8\n";
}
Output
Köln
the file is not UTF-8

Read a utf 16 file

<?nvs
// Shows how to read a file saved as UTF-16, which some Windows programs use for text files.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'names.txt');
// In UTF-16LE, every character here takes two bytes.
array<bytes> $parts = [Core\Encoding::encodeText("Ada\nGrace\n", Core\Charset::Utf16Le)];
Core\IO::writeStream($path, $parts, { overwrite: true });
// This prints "20 bytes on disk".
echo Core\IO::size($path), " bytes on disk\n";
// `readText` converts the bytes to a normal string. This prints "Ada", "Grace" and "10 characters".
string $names = Core\IO::readText($path, { charset: Core\Charset::Utf16Le });
echo $names;
echo Core\Str::length($names), " characters\n";
Output
20 bytes on disk
Ada
Grace
10 characters

Import an old price list

<?nvs
// Shows how a program imports a price list that an older shop system exported in Windows-1252.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'prices.csv');
array<bytes> $export = [Core\Encoding::encodeText("Café au lait;3,50 €\nCrème brûlée;6,00 €\n", Core\Charset::Windows1252)];
Core\IO::writeStream($path, $export, { overwrite: true });
// Windows-1252 has the "€" sign, and Latin-1 does not. So the file is read as `Windows1252`.
string $prices = Core\IO::readText($path, { charset: Core\Charset::Windows1252 });
// Each line has a product and a price, split at ";".
// This prints "Café au lait costs 3,50 €" and "Crème brûlée costs 6,00 €".
foreach (Core\Str::split(Core\Str::trim($prices), "\n") as string $line) {
array<string> $fields = Core\Str::split($line, ';');
echo $fields[0], ' costs ', $fields[1], "\n";
}
Output
Café au lait costs 3,50 €
Crème brûlée costs 6,00 €