Skip to content

Core\IO\File::flush

$file->flush(): void

Sends everything that was written through a handle to the operating system.

$file->flush() works on a Core\IO\File that Core\IO::open returned. In Novis, write already sends the bytes to the operating system before it returns. So another program or a second handle can read the bytes at once, and flush does not change what they see. You can still call flush after a group of writes, and your program keeps working the same way.

flush does not wait until the bytes are stored on the disk. After a power failure, the last writes can still be lost.

A closed handle throws a RuntimeError.

Return value

Returns void. Nothing. This method does not wait until the data is on the disk.

Errors

  • RuntimeErrorThe handle has already been closed.
  • IOErrorThe flush itself failed in the operating system. This is rare, because no data waits in the handle. The operating system may still report an error.

Examples

Flush after writing

<?nvs
// Shows how to call `flush` after writing, and that the text is in the file.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'greeting.txt');
Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Write);
$file->write("Hello\n");
$file->flush();
// The handle is still open. `Core\IO::readText` reads the file and prints "Hello".
echo Core\IO::readText($path);
$file->close();
Output
Hello

A second handle reads without flush

<?nvs
// Shows that a second handle can read what was written, before and after `flush`.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'counter.txt');
Core\IO\File $writer = Core\IO::open($path, Core\IO::FileMode::Write);
$writer->write('42');
// `write` already sent the bytes. The reader gets "42" before any `flush`.
Core\IO\File $reader = Core\IO::open($path, Core\IO::FileMode::Read);
echo 'before flush: ', $reader->read(10), "\n";
$reader->close();
// After `flush` the file is the same. This prints "after flush: 42".
$writer->flush();
$reader = Core\IO::open($path, Core\IO::FileMode::Read);
echo 'after flush: ', $reader->read(10), "\n";
$reader->close();
$writer->close();
// A closed handle cannot flush. `flush` throws a `RuntimeError`.
try {
$writer->flush();
} catch (RuntimeError $e) {
echo "the handle is closed\n";
}
Output
before flush: 42
after flush: 42
the handle is closed

A progress log

<?nvs
// Shows a long job that writes one line per finished step to a log, and flushes after each line.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'import.log');
array<string> $steps = ['customers', 'orders', 'invoices'];
Core\IO\File $log = Core\IO::open($path, Core\IO::FileMode::Write);
foreach ($steps as string $step) {
// A person can watch this log while the job runs. Each line is in the file after `flush`.
$log->write('imported ' . $step . "\n");
$log->flush();
}
$log->close();
// This prints the three lines of the log.
echo Core\IO::readText($path);
Output
imported customers
imported orders
imported invoices