The standard library
Slide 54 of 85
54fileThe standard library

Files

hive.file is the filesystem. Contents move as a Str holding bytes rather than validated text, so a binary file survives a round trip untouched. Everything that can fail answers with a Result<_, hive.file.FileError>, which is NotFound, Permission, Exists or Io, each carrying the path and a message:

  • read(path) is the whole file; lines(path) its lines, a \r\n counting as one ending, the empty piece after a last newline dropped and inner blank lines kept.
  • write(path, contents) and append(path, contents) answer with the number of bytes written. Neither makes a missing folder.
  • writeSecret(path, contents) writes a Secret<Str>, leaving the file readable by this account alone — mode 0600, or a single-account access list on Windows — settled before any of the secret is in it. It keeps the file from other users, not from an administrator, and it refuses a file somebody else owns.
  • copy(from, to) and move(from, to) replace whatever is at to; move is a rename, so it cannot cross from one drive to another.
  • delete(path) removes a file or an empty folder — a folder with something in it is Exists. makeDir(path) makes the folder and every one above it, and is content if it is already there.
  • exists(path) is true for a file or a folder; size(path) is in bytes; list(path) is the bare names in a folder, folders included, sorted.

The table readers — csv, xlsx, ods — live here too, but fail with a hive.file.TableError of their own, carrying only a path and a message.

Writing and reading
proc main(): void {
	hive.file.makeDir("./out")
	hive.file.write("./out/report.txt", "done")
	hive.file.append("./out/report.txt", "\nand done again")

	if hive.file.read("./out/report.txt") is Result.Ok(text) {
		echo text
	}
}
Asking about a file
proc main(): void {
	hive.file.makeDir("./out")
	hive.file.write("./out/report.txt", "done")

	echo hive.file.exists("./out/report.txt")
	if hive.file.size("./out/report.txt") is Result.Ok(n) {
		echo n
	}
	if hive.file.list("./out") is Result.Ok(names) {
		echo names
	}
}