# FileSystemManager.readFileSync(string filePath, string encoding, number position, number length)
同步读取本地文件内容。
# 支持情况
| 项 | 说明 |
|---|---|
| 兼容性 | 兼容微信 |
# 语法
const fs = mt.getFileSystemManager()
const data = fs.readFileSync(filePath, encoding, position, length)
# 参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| filePath | string | 是 | 要读取的文件路径 |
| encoding | string | 否 | 字符编码;不传则以 ArrayBuffer 格式返回 |
| position | number | 否 | 从文件指定位置(字节偏移量)开始读取,默认 0 |
| length | number | 否 | 指定读取的字节数;不传则读取到文件末尾 |
# 返回值
string | ArrayBuffer — 文件内容;传入 encoding 时为 string,否则为 ArrayBuffer。失败时返回错误信息字符串。
# 示例代码
const fs = mt.getFileSystemManager()
const content = fs.readFileSync(`${mt.env.USER_DATA_PATH}/config.json`, 'utf8')
const config = JSON.parse(content)
# 与微信差异
无差异(行为与微信小游戏一致)。