XDPXI's Documentation
Files

File Operations

Required version: Sponge >=1.0.7

Read, write, and manage individual files. The file operations allow you to work with file contents and metadata.

files.read(filepath)

Reads and returns the entire contents of a file.

Parameters:

  • filepath (string) - Path to the file to read (e.g., "data.txt", "config.json")

Returns: string - The file contents, or an error if the file cannot be read

Example:

let content = files.read("hello.txt");
print(content);

Reading JSON:

let config = files.read("config.json");
print(config);

files.write(filepath, content)

Writes content to a file. Creates the file if it doesn't exist, overwrites if it does.

Parameters:

  • filepath (string) - Path to the file to write
  • content (string) - The content to write

Returns: null or error - Returns an error if the operation fails

Example:

files.write("output.txt", "Hello, World!");

Writing Configuration:

let config = "name=MyApp\nversion=1.0\n";
files.write("config.txt", config);

Overwriting a File:

files.write("log.txt", "Log cleared");

files.append(filepath, content)

Appends content to the end of a file. Creates the file if it doesn't exist.

Parameters:

  • filepath (string) - Path to the file to append to
  • content (string) - The content to append

Returns: null or error - Returns an error if the operation fails

Example:

files.append("log.txt", "New log entry\n");

Building a Log File:

files.append("events.log", "Event: User logged in\n");
files.append("events.log", "Event: File downloaded\n");
files.append("events.log", "Event: User logged out\n");

Concatenating Files:

let part1 = files.read("data_part1.txt");
let part2 = files.read("data_part2.txt");
files.write("combined.txt", part1);
files.append("combined.txt", part2);

files.delete(filepath)

Deletes a file. The file must exist or an error is returned.

Parameters:

  • filepath (string) - Path to the file to delete

Returns: null or error - Returns an error if the file cannot be deleted

Example:

files.delete("temp.txt");

Conditional Deletion:

if files.exists("temp.txt") {
    files.delete("temp.txt");
    print("Temporary file deleted");
}

files.exists(path)

Checks if a file or folder exists at the given path.

Parameters:

  • path (string) - Path to check

Returns: boolean - true if the path exists, false otherwise

Example:

if files.exists("data.txt") {
    print("File exists!");
} else {
    print("File not found");
}

Safe File Reading:

let filepath = "config.json";
if files.exists(filepath) {
    let content = files.read(filepath);
    print(content);
} else {
    print("Config file not found");
}