Skip to content

datasource.writeFile ^v2.7.191

Writes data to a file in the MODLR file system. Creates parent directories if they don't exist. The file must be within the root directory of the datasource.

js
datasource.writeFile(fileName, data, append)

Parameters

  • fileName - The name and file path of the file in the MODLR file system to write to. File path can be found in the filesystem page.
  • data - The data to write to the file. Supported types:
    • String - Written as UTF-8 encoded text
    • int[] - Array of integers, each value masked to a single byte (0-255)
    • byte[] - Written directly to the file
    • List - List of numbers, converted to bytes
  • append (optional) - If true, appends data to the end of the file. If false or omitted, overwrites the file. Defaults to false.

Returns

  • boolean - Returns true if the file was written successfully, false if the file path is invalid.

Example

js
// Write text to a new file
datasource.writeFile("output/report.txt", "Hello, World!");

// Append text to an existing file
datasource.writeFile("output/log.txt", "New log entry\n", true);

// Write binary data as an array of integers
let bytes = [72, 101, 108, 108, 111]; // "Hello" in ASCII
datasource.writeFile("output/data.bin", bytes);

// Write JSON data
let data = {
    name: "Budget Report",
    year: 2024,
    values: [100, 200, 300]
};
datasource.writeFile("output/report.json", JSON.stringify(data, null, 2));