writeTextFile
Category: File Operations
Namespace: web.files.writeTextFile
Description
Writes text content to a file. Automatically creates parent directories if they don't exist and uses UTF-8 encoding.
Syntax
const success = await web.files.writeTextFile({
filePath: 'path/to/file.txt',
contents: 'text content'
});
Parameters
- filePath (required) - Path where the file should be saved (relative or absolute)
- contents (required) - Text content to write to the file
Returns
Promise<boolean> - Returns a promise that resolves with true if successful, false otherwise
Examples
Simple example
const success = await web.files.writeTextFile({
filePath: 'output.txt',
contents: 'Hello, World!'
});
if (success) {
console.log('File saved successfully');
}
Save JSON data
const settings = {
theme: 'dark',
language: 'en',
fontSize: 14
};
const success = await web.files.writeTextFile({
filePath: 'config/settings.json',
contents: JSON.stringify(settings, null, 2)
});
Generate report
const report = `Sales Report
Date: ${new Date().toLocaleDateString()}
Total Sales: $10,000
Items Sold: 150`;
await web.files.writeTextFile({
filePath: 'C:\\Reports\\daily_report.txt',
contents: report
});
Use Cases
- Save user preferences and settings
- Export data to JSON, CSV, or text files
- Generate reports and logs
- Create configuration files
- Save user-created content
Notes
- Automatically creates parent directories if they don't exist
- Overwrites existing files without warning
- Uses UTF-8 encoding automatically
- Relative paths are resolved from the application executable directory
- For appending to files, read first, then write combined content