WebExeBuilder Documentation

readFile

Category: Storage

Namespace: web.storage.readFile

Description

Read the text content of a file from the app's dedicated storage folder. Returns null if the file does not exist.

Syntax

const content = await web.storage.readFile({ name: 'filename.ext' });

Parameters

Parameter Type Required Description
name string Yes File name only (e.g. save1.json)

Returns

Promise<string|null> - The file content as a string, or null if the file does not exist

Example

Simple example

const content = await web.storage.readFile({ name: 'notes.txt' });
if (content !== null) {
    console.log(content);
}

Practical example — load a saved game

async function loadGame(slot) {
    const raw = await web.storage.readFile({ name: `save${slot}.json` });
    if (!raw) {
        showMessage(`No save found in slot ${slot}`);
        return;
    }
    const saveData = JSON.parse(raw);
    currentLevel = saveData.level;
    player.health = saveData.health;
    player.position = saveData.position;
    player.inventory = saveData.inventory;
    showMessage(`Loaded save from ${saveData.savedAt}`);
}

Advanced example — load config with defaults

async function loadConfig() {
    const defaults = { theme: 'dark', language: 'en', fontSize: 14 };
    const raw = await web.storage.readFile({ name: 'config.json' });
    if (!raw) return defaults;
    try {
        return { ...defaults, ...JSON.parse(raw) };
    } catch {
        return defaults;
    }
}
  • web.storage.writeFile() - Write a file to the storage folder
  • web.storage.deleteFile() - Delete a file from the storage folder
  • web.storage.listFiles() - List all files in the storage folder

Notes

  • Returns null (not empty string) when a file does not exist — always check before parsing
  • Files are read as UTF-8 text
  • For binary files use web.files.readFileAsBase64() with the full path from getDir()