FileSystemManager.open
Introduction
Opens a file and returns the file descriptor.
Usage Restrictions
Supported by basic library version 3.0.0 or higher.
Parameters
Object object
| Property | Type | Default | Required | Description |
|---|---|---|---|---|
| filePath | string | Yes | File path (relative path) | |
| flag | string | r | No | File system flag, default value: 'r' |
| success | function | No | Callback function executed upon successful call | |
| fail | function | No | Callback function executed upon failed call | |
| complete | function | No | Callback function executed upon completion of call |
Valid Values for File System Flag (flag)
| Value | Description |
|---|---|
| a | Open the file for appending. If the file does not exist, create the file. |
| a+ | Open the file for reading and appending. If the file does not exist, create the file. |
| ax | Similar to 'a', but fails if the path exists. |
| ax+ | Similar to 'a+', but fails if the path exists. |
| w | Open the file for writing. If the file does not exist, create the file. |
| w+ | Open the file for reading and writing. If the file does not exist, create the file. |
| wx | Similar to 'w', but fails if the path exists. |
| wx+ | Similar to 'w+', but fails if the path exists. |
| r | Open the file for reading. If the file does not exist, an exception occurs. |
| r+ | Open the file for reading and writing. If the file does not exist, an exception occurs. |
object.success Callback Function
Parameters
Object res
| Property | Type | Description |
|---|---|---|
| fd | string | File descriptor |
| success | string | true-Operation successful |
object.fail Callback Function
Parameters
Object res
| Property | Type | Description |
|---|---|---|
| errMsg | string | Error message |
| success | string | false-Operation failed |
errMsg Description
| Error message | Description |
|---|---|
| parameter error: F10001 | Invalid parameter |
| fail sdcard not mounted: F10002 | Failed to mount Android sdcard |
| fail no such file or directory: F10007 ${dirPath} | Parent directory does not exist |
Sample Code
// pages/index/index.js
Page({
open() {
const fs = dlt.getFileSystemManager();
fs.open({
filePath: `${dlt.env.USER_DATA_PATH}/hello.txt`,
flag: 'a+',
success: (res) => {
console.log('success', res);
},
fail: (res) => {
console.log('fail', res);
},
complete: (res) => {
console.log('complete', res);
},
});
},
});