Core\IO\File::readLine
$file->readLine(): ?stringReads the next line of an open file.
$file->readLine() works on a Core\IO\File that Core\IO::open returned. It reads from the current position to the end of the line, and moves the position to the start of the next line. The line break is not part of the result. \n, \r\n and \r all end a line, so a file from any system is read the same way. A last line with no line break is still a line.
When the file has no more lines, readLine returns null. A loop that reads until null reads the whole file, and only one line is in memory at a time. An empty line is an empty string, not null.
A closed handle throws a RuntimeError. A line that is not UTF-8 text also throws a RuntimeError, and the position does not move. A handle that can only write throws an IOError.
Return value
Returns ?string. The line without its line ending, or null at the end of the file. Because of the null, there is no separate eof method. A last line with no line ending is still a line.
Errors
RuntimeErrorThe handle has already been closed.RuntimeErrorThe line is not valid UTF-8. The handle does not move, so the next call reads the same line.IOErrorThe read itself failed, or the handle was not opened for reading.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\IO\File::readLine reads ahead into a buffer, so reading a file line by line is faster. Each open file that has read a line uses about 8 KB more memory while it is open. |
0.0.5 | On a file that cannot seek, a method called after Core\IO\File::readLine throws an error. No bytes of the file are lost. |
Examples
Read a file line by line
<?nvs// Shows how to read a text file one line at a time.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'shopping.txt');Core\IO::write($path, "bread\nmilk\napples\n");
Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Read);// `readLine` returns the next line without its line break.// At the end of the file it returns `null`, and the loop stops.?string $line = $file->readLine();while ($line != null) { echo '- ', $line, "\n"; $line = $file->readLine();}$file->close();- bread- milk- applesEvery kind of line break
<?nvs// Shows that `readLine` ends a line at `\n`, at `\r\n` and at `\r`.// Files from Linux, Windows and old Mac systems are read the same way.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'mixed.txt');// Three different line breaks. The last line has no line break at all.Core\IO::write($path, "one\ntwo\r\nthree\rfour");
Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Read);// This prints each line in brackets. No line contains its line break,// and the last line is a line too.?string $line = $file->readLine();while ($line != null) { echo '[', $line, "]\n"; $line = $file->readLine();}$file->close();[one][two][three][four]Find the errors in a log file
<?nvs// Shows how to find the error lines in a log file. The program reads one line// at a time, so a large log file never has to fit in memory.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'app.log');string $quiet = Core\Str::repeat("INFO order saved\n", 5000);Core\IO::write($path, $quiet . "ERROR payment timed out\n" . $quiet . "ERROR disk is full\n");
Core\IO\File $log = Core\IO::open($path, Core\IO::FileMode::Read);uint $number = 0;uint $errors = 0;?string $line = $log->readLine();while ($line != null) { $number = $number + 1; // This prints the line number and the text of each error line. if (Core\Str::startsWith($line, 'ERROR')) { echo 'line ', $number, ': ', $line, "\n"; $errors = $errors + 1; } $line = $log->readLine();}$log->close();echo 'errors: ', $errors, "\n";line 5001: ERROR payment timed outline 10002: ERROR disk is fullerrors: 2