The standard librarySlide 54 of 86
54fileThe standard library

Files

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

  • read(path) is the whole file. lines(path) is its lines: a \r\n counts as one ending, the empty piece after a last newline is dropped, and blank lines inside are kept.
  • write(path, contents) and append(path, contents) answer with the number of bytes written. Neither creates a missing folder.
  • writeSecret(path, contents) writes a Secret<Str> to a file only this account can read — mode 0600, or a single-account access list on Windows — set 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 anything in it is Exists. makeDir(path) creates the folder and every one above it, and succeeds 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 — are here too, but fail with their own hive.file.TableError, 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
	}
}