Core\IO\File::read
Reads a part of an open file as text.
$file->read($max) works on a Core\IO\File that Core\IO::open returned. It reads up to $max bytes from the current position, and moves the position past them. The next read starts where this one stopped. When the file has no more bytes, read returns an empty string.
The result is always whole text. A character such as é uses more than one byte. If $max ends inside a character, read returns the text before it. The next read starts with that character.
A closed handle throws a RuntimeError. Bytes that are not UTF-8 text also throw a RuntimeError, and the position does not move. A handle that can only write throws an IOError.
Parameters
$maxuintThe most bytes to read. Fewer are returned when the file ends first.
Return value
Returns string. The bytes read, as a string. The text always ends with a whole character. When $max ends inside a character, that character stays in the file for the next read. An empty string means the end of the file.
Errors
RuntimeErrorThe handle has already been closed.RuntimeErrorThe bytes are not valid UTF-8, or the next character is longer than$max. The handle does not move, so the next read starts at the same byte.IOErrorThe read itself failed, or the handle was not opened for reading.
Changelog
| Version | Change |
|---|---|
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 the start and then the rest
<?nvs// Shows how to read the first part of a file, and then the rest.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'note.txt');Core\IO::write($path, 'Hello, world!');
Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Read);// `read(5)` reads the first 5 bytes. This prints "Hello".echo $file->read(5), "\n";
// The next `read` starts where the first one stopped. This prints ", world!".echo $file->read(100), "\n";
// The file has no more bytes, so `read` returns an empty string. This prints 0.echo Core\Str::length($file->read(100)), "\n";$file->close();Hello, world!0A character is never cut
<?nvs// Shows that `read` never cuts a character in two.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'cafe.txt');Core\IO::write($path, 'café');
Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Read);// "é" uses 2 bytes, so "café" has 5 bytes. 4 bytes end inside "é".// So `read(4)` returns "caf", and "é" stays in the file.echo $file->read(4), "\n";
// The next `read` starts with "é". This prints "é".echo $file->read(4), "\n";$file->close();caféCopy a large file in parts
<?nvs// Shows how to copy a large file in parts, so the program never has the whole file in memory.
string $dir = Core\IO::createTemporaryDirectory();string $source = Core\Path::join($dir, 'export.csv');string $copy = Core\Path::join($dir, 'export-copy.csv');Core\IO::write($source, Core\Str::repeat("1042,Café Größenwahn,19.90\n", 20000));
Core\IO\File $in = Core\IO::open($source, Core\IO::FileMode::Read);Core\IO\File $out = Core\IO::open($copy, Core\IO::FileMode::Write);uint $parts = 0;
// Each `read` returns up to 64 KB. An empty string means the file has ended.string $part = $in->read(65536);while (Core\Str::length($part) > 0) { $out->write($part); $parts = $parts + 1; $part = $in->read(65536);}$in->close();$out->close();
// This prints how many parts were copied, and that both files have the same size.echo 'parts: ', $parts, "\n";echo 'same size: ', Core\IO::size($source) == Core\IO::size($copy) ? 'yes' : 'no', "\n";parts: 10same size: yes