Filesystem Module

The fs module provides file I/O operations. Read and write files with simple function calls.

fs.readFile(path)

Reads a file and returns its contents as a string:

let content = fs.readFile("data.txt");
print(content);

Throws an error if the file cannot be read.

fs.writeFile(path, content)

Writes a string to a file:

fs.writeFile("output.txt", "Hello, World!");

// Write multiple lines
let data = "Line 1
Line 2
Line 3";
fs.writeFile("lines.txt", data);

Creates the file if it doesn't exist, overwrites if it does. Throws an error if the file cannot be written.

Example: Save and load config

class Config {
    fn save(filename, settings) {
        // Convert dict to simple format
        let content = "name=" + settings["name"] + "
";
        content = content + "value=" + settings["value"];
        fs.writeFile(filename, content);
    }
    
    fn load(filename) {
        return fs.readFile(filename);
    }
}

Error handling

Both functions throw runtime errors on failure (file not found, permission denied, etc.). Errors include the filename and line number for debugging.