Skip to content

Core\IO::write

Saves text or bytes to a file.

Core\IO::write takes a path and the content, which is a string or bytes. It writes the content to the file exactly as it is. If the file does not exist, write creates it. If the file exists, its old content is deleted first. After the call, the file contains only the new content.

Pass bytes for a file that is not text, such as an image or the result of Core\Serialize::encode. Core\IO::readBytes reads it back. To add to the end of a file, use Core\IO::append.

write does not create folders. Core\IO::makeDirectory creates them. write throws an IOError when the operating system cannot write the file. For example, the folder does not exist, the path is a folder, or the disk is read-only. If the call returns without an error, the content is in the file.

A program you start from the command line may write any path. On a server, a program may write only in the folders that Core\IO::createTemporaryDirectory made for it. Any other path needs the fs.write capability. Without it, write throws a RuntimeError before any file is created.

Parameters

  • $pathstringThe file to write. A relative path must be a string written directly in the code. It is joined to the folder of the file that contains it.
  • $contentstring|bytesThe text or bytes to write, written as they are. They become the file's entire content. append is the method that adds to what is already there.

Return value

Returns void. Nothing. A failure throws an error. It never returns false, so a caller that ignores the result does not miss a failure.

Errors

  • RuntimeErrorThe configuration does not grant fs.write for this path. This is checked before anything is created, so the failed write leaves no file behind.
  • IOErrorThe capability allowed the call, but the operating system did not. A directory is missing, the filesystem is read-only, or the process does not have permission.

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

Save a text file

<?nvs
// Shows how to save a text to a file with `Core\IO::write`.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'greeting.txt');
// The file does not exist yet, so `write` creates it.
Core\IO::write($path, "Hello, world!\n");
// `Core\IO::read` returns the content of the file. This prints "Hello, world!".
echo Core\IO::read($path);
Output
Hello, world!

Write replaces the old content

<?nvs
// Shows that `Core\IO::write` replaces the whole content of a file.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'status.txt');
Core\IO::write($path, "The backup is running. Please wait.\n");
// The second text is shorter. Nothing of the first text is left in the file.
// This prints "Backup done.".
Core\IO::write($path, "Backup done.\n");
echo Core\IO::read($path);
// `write` does not create folders. The folder "missing" does not exist,
// so this throws an `IOError` and prints "no such folder".
try {
Core\IO::write(Core\Path::join(Core\IO::createTemporaryDirectory(), 'missing', 'status.txt'), 'x');
} catch (IOError $e) {
echo 'no such folder', "\n";
}
Output
Backup done.
no such folder

Export orders as csv

<?nvs
// Shows how a program exports a list of orders to a CSV file that a spreadsheet can open.
array<string> $customers = ['Anna', 'Ben', 'Clara'];
array<uint> $totals = [120, 45, 300];
// The whole file is built as one text first. Each order is one line.
string $csv = "customer,total\n";
uint $i = 0;
while ($i < Core\Arr::count($customers)) {
$csv = $csv . $customers[$i as int] . ',' . $totals[$i as int] . "\n";
$i = $i + 1;
}
// One call saves the whole file. An export from an earlier run is replaced.
string $dir = Core\Path::join(Core\IO::createTemporaryDirectory(), 'exports');
Core\IO::makeDirectory($dir);
string $path = Core\Path::join($dir, 'orders.csv');
Core\IO::write($path, $csv);
// This prints the header line and one line for each order.
echo Core\IO::read($path);
Output
customer,total
Anna,120
Ben,45
Clara,300