File
File holds the functions of the file system and of handles: open files, the standard streams, and network connections. Every operation that can fail returns a Result with an Error; a read that waits takes a context. A path is used as written: ~ and $NAME are not expanded as a shell expands them, so a path under the home directory is Path.join([try Os.home(), ...]), and a relative path is taken against the working directory. An empty path is Err in every call that takes one; File.exists("") is false. Only File.mkdir creates missing directories: every other call that creates a file, a directory or a link is Err when the directory that would hold it is missing, and those that write a file are Err when a directory is at the path.
Details
- Every function that can fail returns a
Result, and none panics on bad input. A call that takes a path names it in theErr's message; a call on a handle names no target, so add one withResult.wrapErr. - File.read, File.readLine, and File.readAll decode what they read as text, so bytes that are not UTF-8 become U+FFFD. File.readBytes, File.readChunk, and File.readExactly keep the bytes.
- A File.Handle is an open file, a standard stream, a connection from
Net, or a buffer over one, and the same reads and writes serve each. The reads of a handle share one buffer, so lines and bytes can follow each other. - A write is handed to the OS before it returns, so nothing needs a flush, except on a handle from File.buffered, which holds its writes until it is full, flushed, or closed. Handed to the OS is not yet on disk; File.sync waits for storage, for data that must survive a crash or power loss.
- A deferred close cannot report an error. A function that writes ends with an explicit
try File.close(out); the deferred close then does nothing. write,writeBytes,create, andcopyreplace what is at the path. Withreplace: falsethey areErrwhen anything is there.
Basic examples
Write a whole file with File.write and read it back with File.read:
try File.write("hello.txt", "Hello, Ant\n")
print(try File.read("hello.txt"))Hello, AntOpen a handle with File.open, close it with defer, and read it line by line with File.readLine:
let f = try File.open("todo.txt")
defer { File.close(f) }
while true {
let line = try File.readLine(f) ?? break
println("- ${line}")
}- buy milk
- water plantsA failure is an Err that names the path:
println(File.read("missing.txt"))Err{ error: Error{ message: "open missing.txt: no such file or directory" } }Scope
A function of the namespace is called through . on a handle, so f.writeLine(line) is File.writeLine(f, line). A function that takes a handle serves a file and a standard stream alike:
func greet(out: File.Handle, names: Array[string]): Result[void, Error] {
for name in names {
try out.writeLine("Hello, ${name}")
}
Result.ok(void)
}
try greet(Os.stdout(), ["Ann"])
let f = try File.create("greetings.txt")
defer { f.close() }
try greet(f, ["Bob", "Cy"])
try f.close()
print(try File.read("greetings.txt"))Hello, Ann
Hello, Bob
Hello, CyText reads decode, and byte reads keep the bytes as they are:
try File.writeBytes("cafe.txt", Bytes.fromArray([0x63, 0x61, 0x66, 0xE9]))
println(try File.read("cafe.txt"))
println(try File.readBytes("cafe.txt"))caf�
Bytes(63 61 66 e9)The reads of a handle share one buffer, so a header line can come first and the bytes after it:
try File.write("packet.txt", "HEAD 3\nabc")
let f = try File.open("packet.txt")
defer { f.close() }
println(try f.readLine())
println(try f.readExactly(3))HEAD 3
Bytes(61 62 63)A connection is a handle too. This server echoes one line back:
let listener = try Net.listen("127.0.0.1:0")
go {
let conn = Result.unwrap(Net.accept(listener))
defer { conn.close() }
let line = Result.unwrap(conn.readLine()) ?? ""
Result.unwrap(conn.writeLine("echo: ${line}"))
}
let conn = try Net.dial(listener.address())
defer { conn.close() }
try conn.writeLine("ping")
println(try conn.readLine())echo: pingA read of a connection waits for input, and a context gives up the wait. Here the server never writes:
let listener = try Net.listen("127.0.0.1:0")
let conn = try Net.dial(listener.address())
defer { conn.close() }
let ctx = Context.new(timeout: 50ms)
let line = conn.readLine(ctx: ctx)
println(line)
println(Context.reason(ctx))Err{ error: Error{ message: "timeout" } }
timeoutThe handle stays open after such a read, and a later read gets what arrives. A read of a regular file does not wait, so there a context is looked at only before the read begins.
Standard input is a handle from Os.stdin(), read line by line like a file:
let input = Os.stdin()
while true {
let line = try input.readLine() ?? break
println(line.toUpper())
}HELLO
ANTFile.list gives the names in one directory, File.readDir the same entries with their kinds, File.walk the files below it, and File.glob the paths that match a pattern:
try File.mkdir("notes/2026")
try File.write("notes/todo.txt", "")
try File.write("notes/2026/march.txt", "")
try File.write("notes/2026/march.md", "")
println(try File.list("notes"))
println((try File.readDir("notes")).filter({ x.isDir }).map({ x.name }))
println(try File.walk("notes"))
println(try File.glob("**/*.txt", dir: "notes"))["2026", "todo.txt"]
["2026"]
["2026/march.md", "2026/march.txt", "todo.txt"]
["2026/march.txt", "todo.txt"]Functions by job
Types
- File.Handle: an open file, a standard stream, a connection, or a buffer over one
- File.Info: the name, size, kind, modified time, mode, and owner of a file
- File.Entry: the name and kind of an entry of a directory
Whole files
- File.read, File.readBytes: the contents of a file, as text or as bytes
- File.write, File.writeBytes: create a file, or replace what it held
Opening a handle
- File.open: open a file for reading
- File.create: create or truncate a file for writing
- File.append: open a file for writing at its end
- File.buffered: a handle that holds its writes until full, flush, or close
- File.isTerminal: whether a handle is a terminal
- File.close: close a handle, writing out a buffer first
Reading a handle
- File.readLine: the next line, or
nullat the end - File.readAll: the rest of the input as text
- File.readChunk: what is there, up to a number of bytes
- File.readExactly: exactly a number of bytes
Writing a handle
- File.writeLine, File.writeText: text, with or without a newline
- File.writeChunk: bytes as they are
- File.pipe: copy the rest of one handle to another
- File.flush: hand what a buffered handle holds to the OS
- File.sync: flush, then have the OS write the file to storage
Directories and listings
- File.mkdir: make a directory with its parents
- File.list: the names in a directory
- File.readDir: the entries in a directory, each with its kind
- File.walk: every file below a directory
- File.glob: the paths that match a pattern
Metadata
- File.exists: whether anything is at a path
- File.stat: the File.Info of a path
- File.chmod: set the mode
- File.chown: set the owning user and group
- File.touch: set the modified time, creating an empty file when missing
Moving, copying, and removing
- File.rename: move a file or directory
- File.copy: copy one file's contents and mode
- File.copyAll: copy a file or a whole tree
- File.remove: remove a file or an empty directory
- File.removeAll: remove a whole tree
Links
- File.symlink: make a symbolic link
- File.readLink: the target of a link, as stored
- File.realPath: a path made absolute with every link resolved
Temporary files and locks
- File.tempFile, File.tempDir: a new file or directory under a free name
- File.lock: open a file holding its lock, waiting for it
- File.tryLock: the same without waiting, or
nullwhen another handle holds it
Applications
Write many lines through a buffer. The deferred close covers an early return, and the final close reports whether the output was written:
func writeReport(path: string, rows: Array[string]): Result[void, Error] {
let out = File.buffered(try File.create(path))
defer { out.close() }
for row in rows {
try out.writeLine(row)
}
out.close()
}
try writeReport("report.txt", ["Ann 9", "Bob 7"])
print(try File.read("report.txt"))Ann 9
Bob 7Replace a file so that a reader never sees it half-written: write a temporary file beside it, then rename it into place:
func save(path: string, text: string): Result[void, Error] {
let tmp = try File.tempFile(".save-*", dir: Path.dir(path))
defer { File.remove(tmp) }
try File.write(tmp, text)
File.rename(tmp, path)
}
try save("config.txt", "port=8080")
println(try File.read("config.txt"))
println(try File.glob(".save-*", hidden: true))port=8080
[]The rename keeps readers from a half-written file but does not wait for the disk; for a file that must survive a crash, sync it first with File.sync, as Replace a file without a partial write shows.
Count the lines of every text file in a directory:
try File.mkdir("logs")
try File.write("logs/a.txt", "one\ntwo\n")
try File.write("logs/b.txt", "three\n")
let dir = "logs"
for path in try File.glob("*.txt", dir: dir) {
let text = try File.read(Path.join([dir, path]))
println("${path}: ${text.lines().length()}")
}a.txt: 2
b.txt: 1Let one run at a time do the work. A second lock on the file, even in the same process, does not get it while the first is held:
let first = try File.tryLock("job.lock")
let second = try File.tryLock("job.lock")
println(first != null)
println(second == null)true
trueProperties and relations
What File.write writes, File.read reads back, and its size in bytes is what File.stat reports:
let text = "héllo"
try File.write("round.txt", text)
assert((try File.read("round.txt")) == text)
assert((try File.stat("round.txt")).size == text.toBytes().length())File.exists is true after a write and false after a remove:
try File.write("gone.txt", "")
assert(File.exists("gone.txt"))
try File.remove("gone.txt")
assert(!File.exists("gone.txt"))Possible issues
A Result is not the value it holds. Use try or narrow it first:
let text = File.read("notes.txt")
println(text.length())
// error: Result[string, Error] has no field or function lengthFile.remove refuses a directory with entries. File.removeAll removes the tree:
try File.mkdir("build/out")
println(File.remove("build"))
println(File.removeAll("build"))Err{ error: Error{ message: "remove build: directory not empty" } }
Ok{ value: void }replace: false is Err when the file exists, and changes nothing:
try File.write("first.txt", "one")
println(File.write("first.txt", "two", replace: false))
println(try File.read("first.txt"))Err{ error: Error{ message: "open first.txt: file exists" } }
oneA buffered handle neither flushed nor closed loses what it holds when the run ends:
let out = File.buffered(Os.stdout())
try out.writeLine("lost")
println("printed")printedA print writes to the stream under the buffer, so it can come out before text the buffer still holds:
let out = File.buffered(Os.stdout())
try out.writeLine("first")
println("second")
try out.close()second
firstAn error from a handle names no file. Add the target with Result.wrapErr. A handle from File.open is for reading, so a write to it is Err:
try File.write("log.txt", "")
let f = try File.open("log.txt")
defer { f.close() }
println(f.writeLine("x"))
println(f.writeLine("x") | Result.wrapErr("write log.txt"))Err{ error: Error{ message: "the file is not open for writing" } }
Err{ error: Error{ message: "write log.txt: the file is not open for writing" } }See also
Path · Os · Net · Read and write a whole file · Replace a file without a partial write · Walk a directory and match globs
