readTextFile
Category: File Operations
Namespace: web.files.readTextFile
Description
Reads the contents of a text file and returns it as a string. Automatically handles UTF-8 encoding.
Syntax
const content = await web.files.readTextFile({
filePath: 'path/to/file.txt'
});
Parameters
- filePath (required) - Path to the text file to read (relative or absolute)
Returns
Promise<string> - Returns a promise that resolves with the file contents as a string
Examples
Simple example
const content = await web.files.readTextFile({
filePath: 'config.json'
});
console.log('File content:', content);
Read and parse JSON
const jsonContent = await web.files.readTextFile({
filePath: 'data/settings.json'
});
const settings = JSON.parse(jsonContent);
console.log('Theme:', settings.theme);
Read with error handling
try {
const content = await web.files.readTextFile({
filePath: 'C:\\Data\\report.txt'
});
console.log('File loaded successfully');
} catch (error) {
console.error('Failed to read file:', error);
}
Use Cases
- Load configuration files (JSON, XML, INI)
- Read user-created documents
- Load template files for processing
- Read log files for display
- Import data from text files
Notes
- Automatically uses UTF-8 encoding
- Relative paths are resolved from the application executable directory
- Returns an error if the file doesn't exist
- For binary files, use other methods (not text files)
- Newlines are preserved in the returned string