Skip to content

Core\IO::FileMode

What an open file is allowed to do, and what opening it does to whatever is already in the file.

Core\IO::FileMode is declared in the class Core\IO. The method open takes it.

There are four. Read only reads, from a file that must already exist. Write only writes, and empties the file the moment you open it, creating it if it is not there. Append only writes, always at the end, keeps what is there, and creates the file if it is not there. ReadWrite does both, creates the file if it is missing, and empties nothing.

Write is the one to be careful with. The file is empty from the moment the handle exists, whether or not you ever write to it.

Good to know: there is no binary or text flag to choose. Novis text is bytes, so nothing changes line endings for you on the way in or out.

Examples

The four modes and what each does

<?nvs
// Every mode you can open a file with, and what each one does to the file that
// is already there. You pass one to `Core\IO::open` as its second argument.
class Modes
{
public static function effect(Core\IO::FileMode $mode): string
{
return match ($mode) {
Core\IO::FileMode::Read => 'Read: reads only, and the file has to exist already',
Core\IO::FileMode::Write => 'Write: writes only, empties the file on open, creates it if it is missing',
Core\IO::FileMode::Append => 'Append: writes only, always at the end, keeps what is there',
Core\IO::FileMode::ReadWrite => 'ReadWrite: reads and writes, creates the file if it is missing, empties nothing',
};
}
}
array<Core\IO::FileMode> $everything = [
Core\IO::FileMode::Read,
Core\IO::FileMode::Write,
Core\IO::FileMode::Append,
Core\IO::FileMode::ReadWrite,
];
foreach ($everything as Core\IO::FileMode $mode) {
echo Modes::effect($mode), "\n";
}
Output
Read: reads only, and the file has to exist already
Write: writes only, empties the file on open, creates it if it is missing
Append: writes only, always at the end, keeps what is there
ReadWrite: reads and writes, creates the file if it is missing, empties nothing

Picking the mode for the job

<?nvs
// Four everyday jobs and the mode each one wants. The mode is a value, so you
// can decide it in one place and pass it to `Core\IO::open` later.
class Modes
{
public static function name(Core\IO::FileMode $mode): string
{
return match ($mode) {
Core\IO::FileMode::Read => 'Read',
Core\IO::FileMode::Write => 'Write',
Core\IO::FileMode::Append => 'Append',
Core\IO::FileMode::ReadWrite => 'ReadWrite',
};
}
}
// Adding a line to a log that already has today's other lines in it.
Core\IO::FileMode $log = Core\IO::FileMode::Append;
// Writing this morning's report, which replaces yesterday's completely.
Core\IO::FileMode $report = Core\IO::FileMode::Write;
// Reading a settings file the program never writes to.
Core\IO::FileMode $settings = Core\IO::FileMode::Read;
// A small counter file the program reads and then updates, and which is not
// there the first time the program runs.
Core\IO::FileMode $counter = Core\IO::FileMode::ReadWrite;
echo 'append a log line -> ', Modes::name($log), "\n";
echo "write today's report -> ", Modes::name($report), "\n";
echo 'read the settings file -> ', Modes::name($settings), "\n";
echo 'read and bump a count -> ', Modes::name($counter), "\n";
Output
append a log line -> Append
write today's report -> Write
read the settings file -> Read
read and bump a count -> ReadWrite

Checking a plan before it empties a file

<?nvs
// This program checks a nightly job's files before the job opens them.
// It prints which files will be emptied and which must already exist.
// The mode alone decides both, so the check does not read the disk.
class Plan
{
// `Write` empties the file as soon as it is opened, even if nothing is
// written afterwards.
public static function emptiesTheFile(Core\IO::FileMode $mode): bool
{
return $mode == Core\IO::FileMode::Write;
}
// `Read` is the only mode that will not create a file that is missing.
public static function needsTheFileToExist(Core\IO::FileMode $mode): bool
{
return $mode == Core\IO::FileMode::Read;
}
public static function check(string $path, Core\IO::FileMode $mode): string
{
if (Plan::emptiesTheFile($mode)) {
return $path . ": will be emptied first, so keep a copy of last night's";
}
if (Plan::needsTheFileToExist($mode)) {
return $path . ': has to be there already, or the job stops';
}
return $path . ': safe, nothing already in it is lost';
}
}
echo Plan::check('exports/customers.csv', Core\IO::FileMode::Write), "\n";
echo Plan::check('var/log/nightly.log', Core\IO::FileMode::Append), "\n";
echo Plan::check('config/settings.json', Core\IO::FileMode::Read), "\n";
echo Plan::check('var/run/last-export.txt', Core\IO::FileMode::ReadWrite), "\n";
Output
exports/customers.csv: will be emptied first, so keep a copy of last night's
var/log/nightly.log: safe, nothing already in it is lost
config/settings.json: has to be there already, or the job stops
var/run/last-export.txt: safe, nothing already in it is lost