Core\IO::open
Core\IO::open(string $path, Core\IO::FileMode $mode): Core\IO\FileCore\IO::open( string $path, Core\IO::FileMode $mode, ): Core\IO\File
Opens a file and returns a handle that reads or writes it a part at a time.
Core\IO::open takes the path and a Core\IO::FileMode. The mode says what the handle can do:
Readreads a file that must already exist.Writecreates the file, or empties it if it is already there.Appendwrites at the end of the file and keeps what it has.ReadWritereads and writes, creates the file if it is not there, and keeps what it has.
The result is a Core\IO\File. Call close when you are done. Handles that are still open at the end of the program are closed for you.
A program you start from the command line may open any file in every mode. On a server, Read works for the files in the application folder, except nvs.toml and names that start with .. Write and Append work only in the folders that Core\IO::createTemporaryDirectory made. For any other path, Read needs the fs.read capability, Write and Append need fs.write, and ReadWrite needs both. Without it, open throws a RuntimeError. When the file cannot be opened, for example because it does not exist, open throws an IOError.
Parameters
$pathstringThe file to open. A relative path must be a string written directly in the code. It is joined to the folder of the file that contains it.$modeCore\IO::FileModeWhat the handle may do, as aCore\IO::FileModecase.
Return value
Returns Core\IO\File. An open Core\IO\File. It is closed by close, and by the end of the request if the program never calls it.
Errors
RuntimeErrorThe configuration does not grant a capability this mode needs for this path.IOErrorThe capability allowed the call, but the operating system did not. The mode isReadand nothing is at the path, the path is a directory, or this process may not open it.
Examples
Write and read back
<?nvs// Shows how to write a file through a handle, and then read it back through a second handle.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'greeting.txt');
// `FileMode::Write` creates the file, or empties it if it is already there.Core\IO\File $out = Core\IO::open($path, Core\IO::FileMode::Write);$out->write("Hello\n");$out->write("World\n");$out->close();
// `FileMode::Read` opens the file for reading only. `read(100)` returns up to 100 bytes.// This prints "Hello" and "World" on two lines.Core\IO\File $in = Core\IO::open($path, Core\IO::FileMode::Read);echo $in->read(100);$in->close();HelloWorldAppend to a log
<?nvs// Shows how `FileMode::Append` adds to the end of a file and keeps what the file already has.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'visits.log');Core\IO::write($path, "visit 1\n");
// Each open with `Append` writes after the last line. The file is created if it is not there.array<string> $visits = ["visit 2\n", "visit 3\n"];foreach ($visits as string $line) { Core\IO\File $log = Core\IO::open($path, Core\IO::FileMode::Append); $log->write($line); $log->close();}
// This prints "visit 1", "visit 2" and "visit 3".echo Core\IO::readText($path);
// A file that does not exist cannot be opened with `Read`. `open` throws an `IOError`.try { Core\IO::open(Core\Path::join(Core\IO::createTemporaryDirectory(), 'missing.log'), Core\IO::FileMode::Read);} catch (IOError $e) { echo "missing.log cannot be opened\n";}visit 1visit 2visit 3missing.log cannot be openedCopy the errors from a log
<?nvs// Shows how a program reads a log file one line at a time and copies the error lines to a second file.
string $dir = Core\IO::createTemporaryDirectory();string $source = Core\Path::join($dir, 'app.log');string $target = Core\Path::join($dir, 'errors.log');Core\IO::write($source, "INFO start\nERROR disk full\nINFO retry\nERROR disk full\nINFO stop\n");
Core\IO\File $in = Core\IO::open($source, Core\IO::FileMode::Read);Core\IO\File $out = Core\IO::open($target, Core\IO::FileMode::Write);
// `readLine` returns the next line without its line break. At the end of the file it returns `null`.// Only one line is in memory at a time, so this also works for a very large log.?string $line = $in->readLine();uint $copied = 0;while ($line != null) { if (Core\Str::startsWith($line, 'ERROR')) { $out->write($line . "\n"); $copied = $copied + 1; } $line = $in->readLine();}$in->close();$out->close();
// This prints "2 lines copied", and then the two error lines.echo $copied, " lines copied\n";echo Core\IO::readText($target);2 lines copiedERROR disk fullERROR disk full