HomeDocs
Skip to main content

FileSystemManager.read

Introduction

This function is used to read a file.

Usage Restrictions

This function is supported in basic library version 3.0.0 or higher.

Parameters

Object object

PropertyTypeDefaultRequiredDescription
fdstringYesFile descriptor obtained through the FileSystemManager.open interface.
arrayBufferArrayBufferYesBuffer for data to be written, must be an instance of ArrayBuffer.
offsetnumber0NoOffset of the write operation within the buffer, default is 0.
lengthnumber0NoNumber of bytes to read from the file, default is 0.
positionnumber0NoStarting position for reading the file. If not specified or set to null, reading will start from the current file pointer. If the position is a positive integer, the file pointer will remain unchanged and the file will be read from the specified position.
successfunctionNoCallback function for a successful API call.
failfunctionNoCallback function for a failed API call.
completefunctionNoCallback function for the end of the API call (executed regardless of success or failure).

object.success

Callback Function

Parameters

Object res

PropertyTypeDescription
bytesReadnumberNumber of bytes actually read.
arrayBufferArrayBufferBuffer object that was written to, i.e., the arrayBuffer parameter passed to the interface.

object.fail Callback Function

Parameters

Object res

PropertyTypeDescription
errMsgstringError message

errMsg Explanation

Error messageExplanation
parameter error: F10001Invalid parameter
bad file descriptor: F10003Invalid file descriptor
fail permission denied: F10005No read permission for the specified fd path
native buffer exceeds size limit: F10008File size exceeds the limit (100M)

Sample Code

// pages/index/index.js
Page({
read() {
const fs = dlt.getFileSystemManager();
const ab = new ArrayBuffer(1024);
// Open the file
fs.open({
filePath: `${dlt.env.USER_DATA_PATH}/hello.txt`,
flag: 'a+',
success(res) {
// Read the file into the ArrayBuffer
fs.read({
fd: res.fd,
arrayBuffer: ab,
length: 10,
success(res) {
console.log(res);
},
});
},
});
},
});
Privacy agreementDeveloper agreementcontact us: developer_service.mi@transsion.com © 2024 MiniApp. All Rights Reserved.