Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

std/fs

File system operations for reading, writing, and managing files.

Struct: File

Represents a file in the file system.

struct File {
private:
    content: string;
public:
    path: string;
}

Constructor

import "std/fs";

let file = new File("data.txt");

Creates a File object. The file is not opened until read() or open() is called.

Methods

read(&this) -> string

Read the entire content of the file and store it internally.

let file = new File("data.txt");
let content = file.read();
println(content);

open(&this)

Open and read the file, storing content internally.

let file = new File("data.txt");
file.open();
// File content is now loaded

write(&this, content: string)

Write content to the file.

let file = new File("output.txt");
file.write("Hello, World!");

close(this)

Close the file (consumes the File object).

let file = new File("data.txt");
file.read();
file.close();  // File is consumed

exists(&this) -> bool

Check if the file exists.

let file = new File("config.json");
if file.exists() {
    println("File found");
}

remove(&this)

Delete the file from the file system.

let file = new File("temp.txt");
file.remove();

read_dir(&this, path: string) -> [string]

Read the contents of a directory (instance method).

let file = new File(".");
let entries = file.read_dir("./src");

read_file(&this, path: string) -> string

Read a file without modifying the File instance’s content.

let file = new File("dummy");
let content = file.read_file("actual.txt");