File.read
func File.read(path: string): Result[string, Error]File.read reads the whole file at path as text. Bytes that are not valid UTF-8 become U+FFFD; File.readBytes keeps them as they are.
Details
File.readreads the whole file into one string. For a file too large to hold at once, or a stream, open it with File.open and read it with File.readLine or File.readChunk.- A missing file, a directory, or a file the program may not read is
Err, and the message names the path. - To find out whether a file is text, read it with File.readBytes and check it with String.fromBytes.
- A byte order mark at the start is kept, as
"\u{FEFF}". - A symbolic link is followed, and the text is its target's.
Basic examples
println(File.read("notes.txt"))
println(File.read("missing.txt"))Ok{ value: "buy milk\ncall Ann\n" }
Err{ error: Error{ message: "open missing.txt: no such file or directory" } }Inside a function that returns a Result, try gives the text and returns the Err early:
func countLines(path: string): Result[int, Error] {
let text = try File.read(path)
Result.ok(text.lines().length())
}
println(countLines("notes.txt"))
println(countLines("missing.txt"))Ok{ value: 2 }
Err{ error: Error{ message: "open missing.txt: no such file or directory" } }Scope
An empty file
An empty file is Ok with the empty string:
try File.write("empty.txt", "")
println(File.read("empty.txt"))Ok{ value: "" }Unicode
The text comes back as written, whatever the script:
try File.write("greeting.txt", "Привет, 世界 👋")
let text = try File.read("greeting.txt")
println(text)
println(text.length())Привет, 世界 👋
12Bytes that are not UTF-8
Each ill-formed sequence becomes U+FFFD. Here a Latin-1 é (byte E9) and a euro sign cut short (E2 82) become one replacement character each:
try File.writeBytes("latin1.txt", Bytes.fromArray([0x63, 0x61, 0x66, 0xE9, 0x20, 0xE2, 0x82]))
let text = try File.read("latin1.txt")
println(text)
println(text.count("\u{FFFD}"))
println(String.fromBytes(try File.readBytes("latin1.txt")))caf� �
2
nullA directory
A directory is not a file to read:
try File.mkdir("logs")
println(File.read("logs"))Err{ error: Error{ message: "read logs: is a directory" } }A symbolic link
Reading through a link reads the file it points at:
try File.symlink("config.v2.toml", "config.toml")
println(File.read("config.toml"))Ok{ value: "port = 8080\n" }Applications
Process the lines of a file
Split the text with String.lines, which drops the final line break:
let text = try File.read("todo.txt")
for { index, value } in text.lines().entries() {
println("${index + 1}. ${value}")
}1. buy milk
2. call Ann
3. water plantsLoad a configuration file
Read, parse, and say which file failed with Result.wrapErr:
func loadSettings(path: string): Result[Json.Value, Error] {
let text = try (File.read(path) | Result.wrapErr("load settings"))
Json.parse(text) | Result.wrapErr("parse ${path}")
}
println(loadSettings("settings.json").isOk())
println(loadSettings("other.json"))true
Err{ error: Error{ message: "load settings: open other.json: no such file or directory" } }Sum a column of a CSV file
Parse the text with Csv.parse and skip the header row. A row whose amount is not an integer is an Err:
func sumAmounts(path: string): Result[int, Error] {
let text = try File.read(path)
let rows = try Csv.parse(text)
var total = 0
for row in rows.slice(1, rows.length()) {
let amount = row[1].toInt()
if amount == null { return Result.fail("${path}: amount ${row[1]} is not an integer") }
total += amount
}
Result.ok(total)
}
println(sumAmounts("sales.csv"))Ok{ value: 16 }Edit a file in place
Read the text, change it, and write it back with File.write:
let text = try File.read("app.conf")
try File.write("app.conf", text.replace("8080", "9090"))
println(try File.read("app.conf"))host = localhost
port = 9090Properties and relations
What File.write writes, File.read reads back unchanged:
let text = "line one\nline two\n"
try File.write("round.txt", text)
assert(try File.read("round.txt") == text)File.read gives what File.readBytes gives, decoded by String.fromBytesLossy:
try File.writeBytes("mixed.txt", Bytes.fromArray([0x6F, 0x6B, 0xFF, 0x21]))
let bytes = try File.readBytes("mixed.txt")
assert(try File.read("mixed.txt") == String.fromBytesLossy(bytes))Possible issues
The final line break
Most text files end with a line break, so splitting on "\n" gives an empty last element. String.lines does not:
let text = try File.read("names.txt")
println(text.split("\n"))
println(text.lines())["Ann", "Bob", ""]
["Ann", "Bob"]A byte order mark is kept
A file saved with a byte order mark starts with "\u{FEFF}", which makes a comparison of its first line fail. Remove it with trimPrefix:
try File.writeBytes("bom.csv", Bytes.fromArray([0xEF, 0xBB, 0xBF, 0x69, 0x64]))
let text = try File.read("bom.csv")
println(text == "id")
println(text.trimPrefix("\u{FEFF}") == "id")false
trueBinary data is changed
File.read is for text. Bytes that are not UTF-8 are replaced, so writing the text back does not restore the file. Copy binary data with File.readBytes or File.copy:
let original = Bytes.fromArray([0x89, 0x50, 0x4E, 0x47])
try File.writeBytes("image.png", original)
try File.write("copy.png", try File.read("image.png"))
println(try File.readBytes("copy.png") == original)falseA missing file is not told apart in the Err
The Err does not say why the read failed, so a program that treats a missing file differently from other failures checks File.exists first. Here a missing file means no entries, while a directory or a file the program may not read is still an Err:
func loadEntries(path: string): Result[Array[string], Error] {
let text = if File.exists(path) { try File.read(path) } else { "" }
Result.ok(text.lines())
}
println(loadEntries("history.txt"))
try File.mkdir("cache")
println(loadEntries("cache"))Ok{ value: [] }
Err{ error: Error{ message: "read cache: is a directory" } }The result is a Result
The text is inside the Result, so using it directly as a string is a compile error:
let size = File.read("notes.txt").length()
// error: Result[string, Error] has no field or function lengthSee also
File.readBytes · File.write · File.open · File.readLine · File.readAll · File.exists · String.lines · String.fromBytesLossy
