Skip to content

FileWriter

Standard library class · Made with File.create

A FileWriter is a file that is open for writing. File.write puts a whole text in a file in one go; a writer lets a program add to the file as it goes, such as a line for each result of a long calculation, without holding all of it in memory first.

const writer = File.create("squares.txt")
for n in 1..4 {
writer.write("#{n} squared is #{n * n}\n")
}
writer.close()
print(File.read("squares.txt"))
Output
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16

File.create makes the file, or empties it if it was already there. Each write adds to the end of what has been written so far. Nothing is added between writes, so end a line with \n yourself.

A writer only writes. To read a file a piece at a time, use a FileHandle; to add to a file that already has content, use File.append.

Close the writer when you are done, so everything reaches the file. File.with_writer does that for you, even if something goes wrong partway:

File.with_writer("squares.txt", { writer =>
for n in 1..4 {
writer.write("#{n * n}\n")
}
})
print(File.read_lines("squares.txt"))
Output
["1", "4", "9", "16"]
Writing
write(text) Add text to the file
write_bytes(bytes) Add raw bytes to the file
Finishing
close() Finish the file and let go of it

write(text: String)

Adds text to the end of the file, as UTF-8.

Raises a FileError when the text can’t be written, or when the writer has been closed.

write_bytes(bytes: Bytes)

Adds Bytes to the end of the file exactly as they are.

const writer = File.create("hi.bin")
writer.write_bytes(Bytes.from_list([72, 105]))
writer.close()
print(File.read("hi.bin"))
Output
Hi

Raises a FileError when the bytes can’t be written, or when the writer has been closed.

close()

Finishes the file and lets go of it. Closing a writer that is already closed does nothing, so it is safe to close one twice. Writing to a closed writer is an error:

const writer = File.create("note.txt")
writer.close()
writer.write("too late")
Output
note.em:3:1: FileError: cannot write to a closed file
writer.write("too late")
^^^^^^^^^^^^^^^^^^^^^^^^
Check that the path exists and that this program may access it.