I/ONew code should import fs.mod. OsString and Path preserve the length-delimited operating-system path bytes and reject empty, oversized, or NUL-containing values before an ABI call. File is move-only and accepts an OperationContext, Path, OpenOptions, and explicit DirectIoPolicy.

I/O

File I/O

Production filesystem API

New code should import fs.mod. OsString and Path preserve the length-delimited operating-system path bytes and reject empty, oversized, or NUL-containing values before an ABI call. File is move-only and accepts an OperationContext, Path, OpenOptions, and explicit DirectIoPolicy.

The production surface provides checked 64-bit positional reads and writes, metadata, durability, preallocation, truncation, sparse-hole punching, space queries, advisory locks, directory iteration, symbolic links, exclusive temporary files, crash-safe atomic replacement, confined child paths, non-following recursive deletion, and cross-filesystem tree moves. All operations return stable fs.* errors; cleanup is explicit and idempotent.

io.direct adds bounded positional gather reads and writes. Gather functions accept the opaque File.handle value without transferring the move-only File; the caller remains responsible for synchronization and close. Off never asks the operating system for direct I/O. Preferred may fall back only during open and exposes fallbackCount; Required fails if direct I/O cannot be established. Active direct I/O rejects misaligned offsets, lengths, and buffers. Partial completion is reported rather than silently retried.

The older io.file.FsFile and scalar helpers below remain for bootstrap and source compatibility. They are not the production filesystem ownership API.

Production function/typeContract
Path.fromStringPreserve path bytes and reject empty, oversized, or embedded-NUL values
File.openOpen a move-only handle with explicit options and direct-I/O policy
File.readAt / writeAtChecked positional I/O with exact partial progress
Directory.open / nextOwned bounded directory iteration with explicit close
createDirectoryPathContext-aware directory creation for a checked Path
confinedChildReject absolute, empty, dot, and parent traversal components
atomicReplaceAdjacent exclusive temporary file, data sync, rename, and optional directory sync
moveAcrossFilesystemsRename first, then bounded copy/sync/remove only on EXDEV
readGather / writeGatherAt most 64 validated segments with explicit partial completion

High-Level Functions

import io.file
FunctionSignatureDescription
readText(path: String) r: StringRead entire file to string
writeText(path: String, content: String) r: BoolWrite string to file
appendText(path: String, content: String) r: BoolAppend string to file
exists(path: String) r: BoolCheck if file exists
deleteFile(path: String) r: BoolDelete a file
createDirectory(path: String) r: BoolCreate a directory
ensureParentDirectories(path: String) r: BoolCreate parent directories needed for a file path
writeLines(path: String, lines: Array<String>) r: BoolWrite lines with newline separators
size(path: String) r: IntReturn file size, or -1 on failure
hash(path: String) r: StringReturn a stable file-content hash string

Example

let content = readText("config.toml")
println("Config: {content}")

writeText("output.txt", "Hello, World!")

FsFile Class

For more control over file operations:

let file = FsFile.open("data.txt")
let content = file.readContent()
file.closeFile()
MethodReturnDescription
open(path: String)FsFileOpen for reading
create(path: String)FsFileOpen for writing (create/truncate)
readContent()StringRead all content
read_bytes(size: Int)Array<Int>Read N bytes
writeContent(content: String)VoidWrite string
write_bytes(data: Array<Int>)VoidWrite bytes
closeFile()VoidClose file
size()IntGet file size

FsFileResult represents file-open/read outcomes that need to carry success state alongside file data or errors.

Runtime File Functions

FunctionDescription
__OpenFile(path, mode)Open file, return fd (-1 on error)
__ReadFile(fd)Read entire content
__ReadFileBytes(fd, size)Read N bytes
__WriteFile(fd, content)Write string
__WriteFileBytes(fd, bytes)Write byte array
__CloseFile(fd)Close file descriptor
__FileSize(fd)Get file size
__FileError(fd)Get error message
__FileExists(path)Check existence
__DeleteFile(path)Delete file
__CreateDirectory(path)Create directory

Standard I/O

Output

FunctionDescription
println(s: String)Print string with newline
__Print(s: String)Print without newline
__PrintInt(n: Int)Print integer
__PrintFloat(f: Float)Print float
__PrintRaw(s: String)Print without newline (for LSP headers)
__FlushStdout()Flush stdout buffer

Input

FunctionDescription
__ReadStdinLine()Read one line from stdin (blocking)
__ReadStdinBytes(count: Int)Read exactly N bytes from stdin

StdinReader Class

import io.stdio
MethodReturnDescription
nextLine()StringRead next line
isEof()BoolCheck end of input

ContentLengthReader

For LSP/JSON-RPC message framing:

let reader = ContentLengthReader.new()
let message = reader.readMessage()
let err = reader.getLastError()

Buffered I/O

import io.buffered
  • BufferedWriter -- buffered output
  • BufferedReader -- buffered input

Path Operations

import fs.path
FunctionSignatureDescription
isAbsolute(path: String) r: BoolCheck absolute path
normalize(path: String) r: StringNormalize path
joinPath(parts: Array<String>) r: StringJoin path components
basename(path: String) r: StringGet filename
dirname(path: String) r: StringGet directory
pathExtension(path: String) r: StringGet file extension
withoutExtension(path: String) r: StringRemove extension
splitComponents(path: String) r: Array<String>Split into components

Example

let full = joinPath(["home", "user", "docs", "file.txt"])
let dir = dirname(full)       // "home/user/docs"
let file = basename(full)     // "file.txt"
let ext = pathExtension(full) // "txt"
Architected in Kotlin. Rendered with Materia. Powered by Aether.
© 2026 Yousef.