Skip to content

Std\File

File – filesystem operations with async I/O support.

Provides file reading, writing, directory listing, and low-level file handle operations. Async functions (readFile, readFileBytes, readBytes, writeBytes) use io_uring on Linux for non-blocking I/O.

Read the entire contents of a file as a string. Async (io_uring).

import readFile from Std\File in
let contents = readFile "data.txt" in
println contents

Write a string to a file, creating or overwriting it. Async (io_uring). Returns true on success.

import writeFile from Std\File in
writeFile "out.txt" "hello world" # => true

Append a string to a file. Returns true on success.

import appendFile from Std\File in
appendFile "log.txt" "new line\n" # => true

Check whether a file or directory exists at the given path.

import exists from Std\File in
exists "/tmp" # => true

Delete a file. Returns true on success.

import remove from Std\File in
remove "temp.txt" # => true

Returns the size of a file in bytes.

import size from Std\File in
size "data.bin" # => 4096

List directory contents. Returns a sequence of filenames.

import listDir from Std\File in
listDir "/tmp" # => ["file1.txt", "file2.txt", ...]

Returns an Iterator String that yields lines from the file lazily. Uses O(1) memory per element.

import readLines from Std\File in
let iter = readLines "big.csv" in
# consume with iterator protocol

Read the entire file as a byte buffer. Async (io_uring).

import readFileBytes from Std\File in
let buf = readFileBytes "image.png" in
Bytes::length buf

writeFileBytes : String -> ByteArray -> Bool

Section titled “writeFileBytes : String -> ByteArray -> Bool”

Write a byte buffer to a file. Returns true on success.

import writeFileBytes from Std\File in
import fromSeq from Std\ByteArray in
writeFileBytes "out.bin" (fromSeq [0, 1, 2, 3])

openFile : String -> FileMode -> FileHandle

Section titled “openFile : String -> FileMode -> FileHandle”

Open a file with the given mode string ("r", "w", "rw", etc.). Returns a file descriptor (Int).

import openFile, closeFileHandle from Std\File in
let fd = openFile "data.txt" Read in
closeFileHandle fd

The mode is a FileMode ADT (Prelude): Read, Write, ReadWrite, Append.

Close a file descriptor.

Read up to count bytes from a file descriptor. Async (io_uring). Returns a byte buffer.

Write bytes to a file descriptor. Async (io_uring). Returns the number of bytes written.

Seek to a position in a file. whence is a Whence ADT (Prelude): SeekSet (absolute), SeekCur (relative to current), SeekEnd (relative to end). Returns the new position.

import openFile, seek, tell from Std\File in
let fd = openFile "data.bin" "r" in
seek fd 100 "set"

Returns the current position in a file descriptor.

Flush buffered writes for a file descriptor. Returns true on success.

Truncate a file to the given length. Returns true on success.

Read data from a file descriptor in chunks of chunkSize bytes. Returns a handle for chunked reading.