HomeDocs
Skip to main content

FileSystemManager.write

Introduction

Writes to a file.

Usage Restrictions

Supported in basic library version 3.0.0 or higher.

Parameters

Object object

AttributeTypeDefaultRequiredDescription
fdstringYesFile descriptor obtained through the FileSystemManager.open interface.
datastringYesContent to be written, with a type of String (ArrayBuffer is not supported at the moment).
encodingstringutf8NoOnly 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
positionnumber0NoSpecifies 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.
successfunctionNoCallback function called when the interface is successfully called.
failfunctionNoCallback function called when the interface call fails.
completefunctionNoCallback function called when the interface call ends (executed regardless of success or failure).

object.success

Callback Function

Parameters

Object res

AttributeTypeDescription
bytesWrittennumberNumber 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).
successstringtrue - set successfully

object.fail

Callback Function

Parameters

Object res

AttributeTypeDescription
errMsgstringError message
successstringfalse - failed to set

errMsg Explanation

Error MessageDescription
parameter error: F10001Invalid parameter
bad file descriptor: F10003Invalid file descriptor
fail permission denied: F10005The 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);
},
});
},
});
},
});
Privacy agreementDeveloper agreementcontact us: developer_service.mi@transsion.com © 2024 MiniApp. All Rights Reserved.