HomeDocs
Skip to main content

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

PropertyTypeDefaultRequiredDescription
filePathstringYesFile path (relative path)
flagstringrNoFile system flag, default value: 'r'
successfunctionNoCallback function executed upon successful call
failfunctionNoCallback function executed upon failed call
completefunctionNoCallback function executed upon completion of call

Valid Values for File System Flag (flag)

ValueDescription
aOpen 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.
axSimilar to 'a', but fails if the path exists.
ax+Similar to 'a+', but fails if the path exists.
wOpen 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.
wxSimilar to 'w', but fails if the path exists.
wx+Similar to 'w+', but fails if the path exists.
rOpen 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

PropertyTypeDescription
fdstringFile descriptor
successstringtrue-Operation successful

object.fail Callback Function

Parameters

Object res

PropertyTypeDescription
errMsgstringError message
successstringfalse-Operation failed

errMsg Description

Error messageDescription
parameter error: F10001Invalid parameter
fail sdcard not mounted: F10002Failed 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);
},
});
},
});
Privacy agreementDeveloper agreementcontact us: developer_service.mi@transsion.com © 2024 MiniApp. All Rights Reserved.