小程序开放平台

文档中心
基础
路由
跳转
转发
界面
网络
支付
数据缓存
媒体
位置
开放接口
设备
XHSML
性能
第三方平台
文件
removeSavedFile
openDocument
getFileSystemManager
FileSystemManager方法
access
accessSync
appendFile
appendFileSync
copyFile
copyFileSync
getSavedFileList
getFileInfo
mkdir
mkdirSync
readdir
readdirSync
rmdir
rmdirSync
rename
renameSync
readFile
readFileSync
removeSavedFile
stat
statSync
saveFile
saveFileSync
truncate
truncateSync
unlink
unlinkSync
unzip
writeFile
writeFileSync
Stats
FileStats
错误码
数据分析
画布

fs.readdirSync

开发
>
JS API
>
文件
>
FileSystemManager方法
>
readdirSync
>
更新时间:2025-07-23 11:41:06

📌 从基础库 3.125.4 版本开始支持

功能描述

读取文件内容

参数

Object object

属性
类型
默认值
必填
说明
fdstring文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.openSync 接口获得
arrayBufferArrayBuffer数据写入的缓冲区,必须是 ArrayBuffer 实例
offsetnumber0缓冲区中的写入偏移量,默认0
lengthnumber0要从文件中读取的字节数,默认0
positionnumber文件读取的起始位置,如不传或传 null,则会从当前文件指针的位置读取。如果 position 是正整数,则文件指针位置会保持不变并从 position 读取文件。

返回值

ReadResult

文件读取结果

示例代码

javascript
const fs = xhs.getFileSystemManager()
const ab = new ArrayBuffer(1024)
const fd = fs.openSync({
  filePath: `${xhs.env.USER_DATA_PATH}/hello.txt`,
  flag: 'a+'
})
const res = fs.readSync({
  fd: fd,
  arrayBuffer: ab,
  length: 10
})
console.log(res)
该文档是否对您有帮助?