Device.readFile()

Overview

Added in version 2.0.1

Retrieves the contents of a UTF-8 encoded file. If the file is bigger than 128 KiB, the function will return the first 128 KiB of it.

Starting in version 2.1.2, this function can also read the contents of a file included in project resources. Starting in 2.1.17, the preferred way to access resources changed from res://velocity/resources to velocity://resources/.

Format

var fileContent = Device.readFile(path);

Parameter Description Type Required Notes
path Full file path for the file system, or for files included in the project resources. String Required Use velocity://resources/ to read from a file included in the project resources.
fileContent The contents of the file. String Return value If the file does not exist or does not use UTF-8 encoding, the function returns an empty string.

Example 1

Copy
/* Read file content from the specified path.
*/
var fileContent = Device.readFile("/sdcard/Download/FileToRead.txt");

Example 2

Copy
/* Read file content from a file included in the project resources.
*/
var fileContent = Device.readFile("velocity://resources/FileToRead.txt");

Example 3

Copy
/* Read file content from a file in a Windows directory.
*/
var fileContent = Device.readFile("C:\\ProgramData\\Wavelink\\Velocity\\myfile.txt");