Starting from version 1078, the API prefix has been changed from hbs to qg. The original prefix hbs is still supported.
| API | Description |
|---|---|
| Obtains the globally unique file manager. The FileSystemManager object is returned. | |
| Checks whether a file or directory exists. | |
| Checks whether a file or directory exists in synchronous mode. | |
| Copies a file. | |
| FileSystemManager.copyFileSync(string srcPath, string destPath) | Copies a file in synchronous mode. |
| Creates a directory. | |
| FileSystemManager.mkdirSync(string dirPath, boolean recursive) | Creates a directory in synchronous mode. |
| Deletes a directory. | |
| Deletes a directory in synchronous mode. | |
| Reads the file list of a directory. | |
| Reads the file list of a directory in synchronous mode. | |
| Reads the content of a local file. | |
| string|ArrayBuffer FileSystemManager.readFileSync(string filePath, string encoding) | Reads the content of a local file in synchronous mode. |
| Renames a file. This can also be used to move a file from oldPath to newPath. | |
| FileSystemManager.renameSync(string oldPath, string newPath) | Renames a file in synchronous mode. This can also be used to move a file from oldPath to newPath in synchronous mode. |
| Obtains the stats object of a file. | |
| Stats|Array FileSystemManager.statSync(string path, boolean recursive) | Obtains the stats object of a file in synchronous mode. |
| Deletes a file. | |
| Deletes a file in synchronous mode. | |
| Decompresses a file. | |
| Writes data to a file. The original content will be overwritten by the new content. | |
| FileSystemManager.writeFileSync(string filePath, string|ArrayBuffer data, string encoding) | Writes data to a file in synchronous mode. |
| Saves a temporary file to a local path. | |
| FileSystemManager.saveFileSync(string tempFilePath, string filePath) | Saves a temporary file to a local path in synchronous mode. |
| Appends content to a file. | |
| Appends content to a file in synchronous mode. | |
| Obtains information about a local temporary file or user file. | |
| Deletes local cache files of a quick game. | |
| Reads the content of a local file with a specified compression type. | |
| Reads the content of a local file with a specified compression type in synchronous mode. | |
| Object that describes the file status. | |
| Each FileStats object contains path and Stats. |
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| path | string | Yes | Path of the file or directory, whose existence is to be checked. |
| success | function | No | Callback function triggered upon a successful API call. |
| fail | function | No | Callback function triggered upon a failed API call. For details, please refer to Parameters in the fail callback function. |
| complete | function | No | If a function is passed, it will be called regardless of whether the API call succeeds or fails. |
var fileSystemManager = qg.getFileSystemManager();
fileSystemManager.access({
path : "file path",
success : function() {
console.log("access success" );
},
fail : function(data) {
console.log("access fail " + JSON.stringify(data));
},
complete : function() {
console.log("access complete" );
}
})Checks whether a file or directory exists in synchronous mode.
| Parameter | Type | Description |
|---|---|---|
| path | string | Path of the file or directory, whose existence is to be checked. |
| Result Code | Message | Description |
|---|---|---|
| 1 | no such file or directory | The file or directory does not exist. |
var fileSystemManager = qg.getFileSystemManager();
try {
fileSystemManager.accessSync('file path');
} catch (error) {
console.log('error = ' + error);
}| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| srcPath | string | Yes | Source file path. Only local files are supported. If the source file is not a local one, call the FileSystemManager.saveFile API to save the file to a local path. |
| destPath | string | Yes | Target file path. |
| success | function | No | Callback function triggered upon a successful API call. |
| fail | function | No | Callback function triggered upon a failed API call. For details, please refer to Parameters in the fail callback function. |
| complete | function | No | If a function is passed, it will be called regardless of whether the API call succeeds or fails. |
var fileSystemManager = qg.getFileSystemManager();
fileSystemManager.copyFile({
srcPath : 'source file', // Source file path, which can only refer to a regular file.
destPath : 'target path',
success : function() {
console.log("copy success" );
},
fail : function(data) {
console.log("copy fail " + JSON.stringify(data));
},
complete : function() {
console.log("copy complete" );
}
})| Parameter | Type | Description |
|---|---|---|
| srcPath | string | Source file path. Only local files are supported. If the source file is not a local one, call the FileSystemManager.saveFile API to save the file to a local path. |
| destPath | string | Target file path. |
| Result Code | Message | Description |
|---|---|---|
| 1 | no such file or directory | The source file does not exist, or the upper-level directory of the target file path does not exist. |
| 2 | permission denied | You have no write permission on the target file path. |
var fileSystemManager = qg.getFileSystemManager();
try {
fileSystemManager.copyFileSync('file path', 'dest path');
} catch (error) {
console.log('error = ' + error);
}| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| dirPath | string | Yes | Directory path. |
| recursive | boolean | No | Indicates whether to create a directory after recursively creating its upper-level directory. If the upper-level directory already exists, it will not be created again. The default value is false. |
| success | function | No | Callback function triggered upon a successful API call. |
| fail | function | No | Callback function triggered upon a failed API call. For details, please refer to Parameters in the fail callback function. |
| complete | function | No | If a function is passed, it will be called regardless of whether the API call succeeds or fails. |
var fileSystemManager = qg.getFileSystemManager();
fileSystemManager.mkdir({
dirPath: 'file path',
success : function() {
console.log("mkdir success" );
},
fail : function(data) {
console.log("mkdir fail " + JSON.stringify(data));
},
complete : function() {
console.log("mkdir complete" );
}
})| Parameter | Type | Description |
|---|---|---|
| dirPath | string | Directory path. |
| recursive | boolean | Indicates whether to create a directory after recursively creating its upper-level directory. If the upper-level directory already exists, it will not be created again. The default value is false. |
| Result Code | Message | Description |
|---|---|---|
| 1 | no such file or directory | The upper-level directory does not exist. |
| 2 | permission denied | You have no write permission on the target file path. |
| 3 | file already exists | A file or directory with the same name exists. |
var fileSystemManager = qg.getFileSystemManager();
try {
fileSystemManager.mkdirSync('dir path', false);
} catch (error) {
console.log('error = ' + error);
}| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| dirPath | string | Yes | Path of the directory to be deleted. |
| recursive | boolean | No | Indicates whether to delete a directory recursively. If the value is true, the directory and all of its subdirectories and files will be deleted. The default value is false. |
| success | function | No | Callback function triggered upon a successful API call. |
| fail | function | No | Callback function triggered upon a failed API call. For details, please refer to Parameters in the fail callback function. |
| complete | function | No | If a function is passed, it will be called regardless of whether the API call succeeds or fails. |
var fileSystemManager = qg.getFileSystemManager();
fileSystemManager.rmdir({
dirPath : 'file path',
success : function() {
console.log("rmdir success");
},
fail : function(data) {
console.log("rmdir fail " + JSON.stringify(data));
},
complete : function() {
console.log("rmdir complete" );
}
})| Parameter | Type | Description |
|---|---|---|
| dirPath | string | Path of the directory to be deleted. |
| recursive | boolean | Indicates whether to delete a directory recursively. If the value is true, the directory and all of its subdirectories and files will be deleted. The default value is false. |
| Result Code | Message | Description |
|---|---|---|
| 1 | no such file or directory | The directory does not exist. |
| 7 | directory not empty | The directory is not empty. |
var fileSystemManager = qg.getFileSystemManager();
try {
fileSystemManager.rmdirSync('dir path', false);
} catch (error) {
console.log('error = ' + error);
}| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| dirPath | string | Yes | Path of the directory to be read. |
| success | function | No | Callback function triggered upon a successful API call. For details, please refer to Parameters in the success callback function. |
| fail | function | No | Callback function triggered upon a failed API call. For details, please refer to Parameters in the fail callback function. |
| complete | function | No | If a function is passed, it will be called regardless of whether the API call succeeds or fails. |
var fileSystemManager = qg.getFileSystemManager();
fileSystemManager.readdir({
dirPath : 'file path',
success : function(res) {
console.log("readdir success res = " + JSON.stringify(res));
},
fail : function(data) {
console.log("readdir fail " + JSON.stringify(data));
},
complete : function() {
console.log("readdir complete" );
}
})| Parameter | Type | Description |
|---|---|---|
| dirPath | string | Path of the directory to be read. |
| Result Code | Message | Description |
|---|---|---|
| 1 | no such file or directory | The directory does not exist. |
var fileSystemManager = qg.getFileSystemManager();
try {
var result = fileSystemManager.readdirSync('dir path');
console.log('result = ' + JSON.stringify(result));
} catch (error) {
console.log('error = ' + error);
}| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| filePath | string | Yes | Path of the file to be read. NOTE: The path cannot contain special characters such as the at sign (@), number sign (#), and percent sign (%). |
| encoding | string | No | Character encoding for the file to be read. The valid values are binary and utf8, with binary being the default value. |
| success | function | No | Callback function triggered upon a successful API call. For details, please refer to Parameters in the success callback function. |
| fail | function | No | Callback function triggered upon a failed API call. For details, please refer to Parameters in the fail callback function. |
| complete | function | No | If a function is passed, it will be called regardless of whether the API call succeeds or fails. |
var fileSystemManager = qg.getFileSystemManager();
fileSystemManager.readFile({
filePath : 'file path',
success : function(res) {
console.log("readFile success res = " + JSON.stringify(res));
},
fail : function(data) {
console.log("readFile fail " + JSON.stringify(data));
},
complete : function() {
console.log("readFile complete" );
}
})| Parameter | Type | Description |
|---|---|---|
| filePath | string | Path of the file to be read. NOTE: The path cannot contain special characters such as the at sign (@), number sign (#), and percent sign (%). |
| encoding | string | Character encoding for the file to be read. The valid values are binary and utf8. |
| Result Code | Message | Description |
|---|---|---|
| 1 | no such file or directory | The directory does not exist. |
var fileSystemManager = qg.getFileSystemManager();
try {
var result = fileSystemManager.readFileSync('file path', 'binary');
console.log('result = ' + JSON.stringify(result));
} catch (error) {
console.log('error = ' + error);
}Renames a file. This can also be used to move a file from oldPath to newPath.
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| oldPath | string | Yes | Source file path, which can refer to either a regular file or a directory. |
| newPath | string | Yes | New file path. |
| success | function | No | Callback function triggered upon a successful API call. |
| fail | function | No | Callback function triggered upon a failed API call. For details, please refer to Parameters in the fail callback function. |
| complete | function | No | If a function is passed, it will be called regardless of whether the API call succeeds or fails. |
var fileSystemManager = qg.getFileSystemManager();
fileSystemManager.rename({
oldPath : 'file path',
newPath : 'target path',
success : function() {
console.log("rename success" );
},
fail : function(data) {
console.log("rename fail " + JSON.stringify(data));
},
complete : function() {
console.log("rename complete" );
}
})Renames a file in synchronous mode. This can also be used to move a file from oldPath to newPath in synchronous mode.
| Parameter | Type | Description |
|---|---|---|
| oldPath | string | Source file path, which can refer to either a regular file or a directory. |
| newPath | string | New file path. |
| Result Code | Message | Description |
|---|---|---|
| 1 | no such file or directory | The source file does not exist, or the upper-level directory of the target file path does not exist. |
| 2 | permission denied | You do not have the write permission. |
var fileSystemManager = qg.getFileSystemManager();
try {
fileSystemManager.renameSync('old path', 'new path');
} catch (error) {
console.log('error = ' + error);
}| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| path | string | Yes | Path of a file or directory. |
| recursive | boolean | No | Indicates whether to recursively obtain the stats information of each file in the directory. The default value is false. |
| success | function | No | Callback function triggered upon a successful API call. For details, please refer to Parameters in the success callback function. |
| fail | function | No | Callback function triggered upon a failed API call. For details, please refer to Parameters in the fail callback function. |
| complete | function | No | If a function is passed, it will be called regardless of whether the API call succeeds or fails. |
var fileSystemManager = qg.getFileSystemManager();
fileSystemManager.stat({
path: 'file path',
success : function(res) {
// When recursive is set to false, res.stats is a Stats object.
var st = res.stats;
},
fail : function(data) {
console.log("rename fail " + JSON.stringify(data));
},
complete : function() {
console.log("stat complete" );
}
})| Parameter | Type | Description |
|---|---|---|
| path | string | Path of a file or directory. |
| recursive | boolean | Indicates whether to recursively obtain the stats information of each file in the directory. |
| Result Code | Message | Description |
|---|---|---|
| 1 | no such file or directory | The file does not exist. |
var fileSystemManager = qg.getFileSystemManager();
try {
var result = fileSystemManager.statSync('path', false);
console.log('result = ' + JSON.stringify(result));
} catch (error) {
console.log('error = ' + error);
}| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| filePath | string | Yes | Path of the file to be deleted. |
| success | function | No | Callback function triggered upon a successful API call. |
| fail | function | No | Callback function triggered upon a failed API call. For details, please refer to Parameters in the fail callback function. |
| complete | function | No | If a function is passed, it will be called regardless of whether the API call succeeds or fails. |
var fileSystemManager = qg.getFileSystemManager();
fileSystemManager.unlink({
filePath : 'file path',
success : function() {
console.log("unlink success" );
},
fail : function(data) {
console.log("unlink fail " + JSON.stringify(data));
},
complete : function() {
console.log("unlink complete" );
}
})| Parameter | Type | Description |
|---|---|---|
| filePath | string | Path of the file to be deleted. |
| Result Code | Message | Description |
|---|---|---|
| 1 | no such file or directory | The file does not exist. |
var fileSystemManager = qg.getFileSystemManager();
try {
fileSystemManager.unlinkSync('path');
} catch (error) {
console.log('error = ' + error);
}| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| zipFilePath | string | Yes | Source file path, which can only refer to a ZIP file. |
| targetPath | string | Yes | Target directory path. |
| success | function | No | Callback function triggered upon a successful API call. |
| fail | function | No | Callback function triggered upon a failed API call. For details, please refer to Parameters in the fail callback function. |
| complete | function | No | If a function is passed, it will be called regardless of whether the API call succeeds or fails. |
var fileSystemManager = qg.getFileSystemManager();
fileSystemManager.unzip({
zipFilePath: 'file path',
targetPath: 'target_path',
success : function() {console.log("unzip success" );},
fail : function(data) {console.log("unzip fail " + JSON.stringify(data));},
complete : function() {console.log("unzip complete" );}
})Writes data to a file. The original content will be overwritten by the new content.
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| filePath | string | Yes | Path of the target file. |
| data | string/arraybuffer | Yes | Text or binary data to be written. |
| encoding | string | No | Character encoding to be used. The valid values are utf8 and binary, with utf8 being the default value. |
| success | function | No | Callback function triggered upon a successful API call. |
| fail | function | No | Callback function triggered upon a failed API call. For details, please refer to Parameters in the fail callback function. |
| complete | function | No | If a function is passed, it will be called regardless of whether the API call succeeds or fails. |
var fileSystemManager = qg.getFileSystemManager();
fileSystemManager.writeFile({
filePath : 'file path',
data: 'data string or arraybuffer',
success : function() {
console.log("writeFile success" );
},
fail : function(data) {
console.log("writeFile fail " + JSON.stringify(data));
},
complete : function() {
console.log("writeFile complete" );
}
})| Parameter | Type | Description |
|---|---|---|
| filePath | string | Path of the target file. |
| data | string/arraybuffer | Text or binary data to be written. |
| encoding | string | Character encoding to be used. The valid values are utf8 and binary. |
| Result Code | Message | Description |
|---|---|---|
| 1 | no such file or directory | The specified file or directory does not exist. |
| 2 | permission denied | You have no write permission on the target file path. |
var fileSystemManager = qg.getFileSystemManager();
try {
fileSystemManager.writeFileSync('file path', 'data', 'binary');
} catch (error) {
console.log('error = ' + error);
}Saves a temporary file to a local path. This API moves the temporary file. Therefore, after this API is successfully called, tempFilePath will become unavailable.
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| tempFilePath | string | Yes | Path for storing temporary files. |
| filePath | string | Yes | Path of the file to be saved. |
| success | function | No | Callback function triggered upon a successful API call. For details, please refer to Parameters in the success callback function. |
| fail | function | No | Callback function triggered upon a failed API call. For details, please refer to Parameters in the fail callback function. |
| complete | function | No | If a function is passed, it will be called regardless of whether the API call succeeds or fails. |
var fileSystemManager = qg.getFileSystemManager();
fileSystemManager.saveFile({
tempFilePath: 'temp file path',
filePath: 'target file path',
success : function(res) {
console.log("saveFile success res = " + JSON.stringify(res));
},
fail : function(data) {
console.log("saveFile fail " + JSON.stringify(data));
},
complete : function() {
console.log("saveFile complete" );
}
})Saves a temporary file to a local path in synchronous mode. This API moves the temporary file. Therefore, after this API is successfully called, tempFilePath will become unavailable.
| Parameter | Type | Description |
|---|---|---|
| tempFilePath | string | Path for storing temporary files. |
| filePath | string | Path of the file to be saved. |
| Result Code | Message | Description |
|---|---|---|
| 1 | no such file or directory | The specified file or directory does not exist. |
| 2 | permission denied | You have no write permission on the target file path. |
var fileSystemManager = qg.getFileSystemManager();
try {
fileSystemManager.saveFileSync('temp file path', 'file path');
} catch (error) {
console.log('error = ' + error);
}| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| filePath | string | Yes | Path of the target file. |
| data | string/arraybuffer | Yes | Text or binary data to be written. |
| encoding | string | No | Character encoding to be used. The valid values are binary and utf8, with utf8 being the default value. |
| success | function | No | Callback function triggered upon a successful API call. |
| fail | function | No | Callback function triggered upon a failed API call. For details, please refer to Parameters in the fail callback function. |
| complete | function | No | If a function is passed, it will be called regardless of whether the API call succeeds or fails. |
var fileSystemManager = qg.getFileSystemManager();
fileSystemManager.appendFile({
filePath: 'file path',
data: 'data',
encoding: 'binary',
success : function() {
console.log("appendFile success ");
},
fail : function(data) {
console.log("appendFile fail " + JSON.stringify(data));
},
complete : function() {
console.log("appendFile complete" );
}
})| Parameter | Type | Description |
|---|---|---|
| filePath | string | Path of the target file. |
| data | string/arraybuffer | Text or binary data to be written. |
| encoding | string | Character encoding to be used. The valid values are binary and utf8. |
| Result Code | Message | Description |
|---|---|---|
| 1 | no such file or directory | The specified file or directory does not exist. |
| 2 | permission denied | You have no write permission on the target file path. |
var fileSystemManager = qg.getFileSystemManager();
try {
fileSystemManager.appendFileSync('file path', 'data', 'binary');
} catch (error) {
console.log('error = ' + error);
}Obtains information about a local temporary file or user file.
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| filePath | string | Yes | Path of the file to be read. |
| success | function | No | Callback function triggered upon a successful API call. For details, please refer to Parameters in the success callback function. |
| fail | function(object res) | No | Callback function triggered upon a failed API call. For details, please refer to Parameters in the fail callback function. |
| complete | function | No | If a function is passed, it will be called regardless of whether the API call succeeds or fails. |
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| filePath | string | Yes | Path of the file to be deleted. |
| success | function | No | Callback function triggered upon a successful API call. |
| fail | function(Object res) | No | Callback function triggered upon a failed API call. For details, please refer to Parameters in the fail callback function. |
| complete | function | No | If a function is passed, it will be called regardless of whether the API call succeeds or fails. |
Reads the content of a local file with a specified compression type.
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| filePath | string | Yes | Path of the target file. |
| string | Yes | File compression type. Currently, only br (indicating Brotli compressed files) is supported. | |
| success | function | No | Callback function triggered upon a successful API call. For details, please refer to Parameters in the success callback function. |
| fail | function | No | Callback function triggered upon a failed API call. For details, please refer to Parameters in the fail callback function. |
| complete | function | No | Callback function triggered after an API call is complete. If a function is passed, it will be called regardless of whether the API call succeeds or fails. |
var fileSystemManager = qg.getFileSystemManager();
fileSystemManager.readCompressedFile({
filePath :`${rt.env.USER_DATA_PATH}/test.br`,
compressionAlgorithm: 'br',
success : function(res) {
console.log("readCompressedFile success data: " + JSON.stringify(res?.data));
},
fail : function(data) {
console.log("readCompressedFile fail: " + JSON.stringify(data));
},
complete : function() {
console.log("readCompressedFile complete");
}
})Reads the content of a local file with a specified compression type in synchronous mode.
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| filePath | string | Yes | Path of the target file. |
| compressionAlgorithm | string | Yes | File compression type. Currently, only br (indicating Brotli compressed files) is supported. |
| Parameter | Type | Description |
|---|---|---|
| data | arraybuffer | File content. |
var fileSystemManager = qg.getFileSystemManager();
try {
var result = fileSystemManager.readCompressedFileSync({
filePath :`${rt.env.USER_DATA_PATH}/test.br`,
compressionAlgorithm: 'br',
});
console.log("readCompressedFileSync success data: " + JSON.stringify(result));
} catch (error) {
console.log('readCompressedFileSync error: ' + JSON.stringify(error));
}| Attribute | Type | Description |
|---|---|---|
| mode | string | File type and access permission, corresponding to POSIX stat.st_mode. |
| size | number | File size, in bytes. The parameter corresponds to POSIX stat.st_size. NOTE: If minPlatformVersion specified in manifest.json is greater or equal to 1126, this parameter returns the exact file size in bytes. If the specified minPlatformVersion is less than 1126, the value returned by this parameter is one byte greater than the actual file size. |
| lastAccessedTime | number | Time when a file was last accessed or executed. The time is a Unix timestamp, corresponding to POSIX stat.st_atime. |
| lastModifiedTime | number | Time when a file was last modified. The time is a Unix timestamp, corresponding to POSIX stat.st_mtime. |
| Method | Description |
|---|---|
| boolean Stats.isDirectory() | Checks whether the current object is a directory. |
| boolean Stats.isFile() | Checks whether the current object is a regular file. |
| Attribute | Type | Description |
|---|---|---|
| path | string | Path of a file or directory. |
| stats | Stats object, which describes the file status. |
| Result Code | Message | Description |
|---|---|---|
| -1 | unknown error occurred | Unknown error. |
| 0 | file operate success | Success. |
| 1 | no such file or directory | The file or directory cannot be found. |
| 2 | permission denied | Incorrect permission. |
| 3 | file already exists | The file already exists. |
| 4 | not a directory | Not a file path. |
| 5 | not a file | Not a file. |
| 6 | the maximum size of the file storage limit is exceeded | The file storage space limit is exceeded. |
| 7 | directory not empty | The directory is not empty. |
| 8 | create directory fail | Failed to create the file path. |
| 9 | unsupported compression algorithm | The file compression type is not supported. |
| 10 | decompress fail | Decompression failed. For example, the compressed file is damaged, or the compression algorithm used by the compressed file is different from the input compressionAlgorithm. |