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
| Property | Type | Default | Required | Description | 
|---|---|---|---|---|
| fd | string | Yes | File descriptor obtained through the FileSystemManager.open interface. | |
| arrayBuffer | ArrayBuffer | Yes | Buffer for data to be written, must be an instance of ArrayBuffer. | |
| offset | number | 0 | No | Offset of the write operation within the buffer, default is 0. | 
| length | number | 0 | No | Number of bytes to read from the file, default is 0. | 
| position | number | 0 | No | Starting 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. | 
| success | function | No | Callback function for a successful API call. | |
| fail | function | No | Callback function for a failed API call. | |
| complete | function | No | Callback function for the end of the API call (executed regardless of success or failure). | 
object.success
Callback Function
Parameters
Object res
| Property | Type | Description | 
|---|---|---|
| bytesRead | number | Number of bytes actually read. | 
| arrayBuffer | ArrayBuffer | Buffer object that was written to, i.e., the arrayBuffer parameter passed to the interface. | 
object.fail Callback Function
Parameters
Object res
| Property | Type | Description | 
|---|---|---|
| errMsg | string | Error message | 
errMsg Explanation
| Error message | Explanation | 
|---|---|
| parameter error: F10001 | Invalid parameter | 
| bad file descriptor: F10003 | Invalid file descriptor | 
| fail permission denied: F10005 | No read permission for the specified fd path | 
| native buffer exceeds size limit: F10008 | File 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);
          },
        });
      },
    });
  },
});