FileSystemManager.write
Introduction
Writes to a file.
Usage Restrictions
Supported in basic library version 3.0.0
or higher.
Parameters
Object object
Attribute | Type | Default | Required | Description |
---|---|---|---|---|
fd | string | Yes | File descriptor obtained through the FileSystemManager.open interface. | |
data | string | Yes | Content to be written, with a type of String (ArrayBuffer is not supported at the moment). | |
encoding | string | utf8 | No | Only effective when the data type is String, specifies the character encoding of the file to be written, defaulting to utf8. Valid values: 1. ascii 2. base64 3. binary 4. hex 5. ucs2 (read in little endian) 6. ucs-2 (read in little endian) 7. utf16le (read in little endian) 8. utf-16le (read in little endian) 9. utf-8 10. utf8 11. latin1 |
position | number | 0 | No | Specifies the offset from the beginning of the file where the data is to be written. When position is not passed or a non-Number value is passed, the data will be written at the current pointer position. |
success | function | No | Callback function called when the interface is successfully called. | |
fail | function | No | Callback function called when the interface call fails. | |
complete | function | No | Callback function called when the interface call ends (executed regardless of success or failure). |
object.success
Callback Function
Parameters
Object res
Attribute | Type | Description |
---|---|---|
bytesWritten | number | Number of bytes actually written to the file (note that the number of bytes written may not be the same as the number of characters in the written string). |
success | string | true - set successfully |
object.fail
Callback Function
Parameters
Object res
Attribute | Type | Description |
---|---|---|
errMsg | string | Error message |
success | string | false - failed to set |
errMsg Explanation
Error Message | Description |
---|---|
parameter error: F10001 | Invalid parameter |
bad file descriptor: F10003 | Invalid file descriptor |
fail permission denied: F10005 | The specified fd path does not have read permission |
the named is ${encoding} charset is not supported! | The current character encoding is not supported |
Sample Code
// pages/index/index.js
Page({
write() {
const fs = dlt.getFileSystemManager();
// Open the file
fs.open({
filePath: `${dlt.env.USER_DATA_PATH}/hello.txt`,
flag: 'a+',
success(res) {
// Write to the file
fs.write({
fd: res.fd,
data: 'some text',
success(res) {
console.log(res.bytesWritten);
},
});
},
});
},
});