# FileSystemManager.readFile(Object object)

异步读取本地文件内容。

# 支持情况

项 说明
兼容性 兼容微信

# 语法

const fs = mt.getFileSystemManager()
fs.readFile({ filePath, encoding, position, length, success, fail, complete })

# 参数

Object object

属性 类型 默认值 必填 说明
filePath string — 是 要读取的文件路径
encoding string — 否 指定读取文件的字符编码;不传则以 ArrayBuffer 格式返回
position number 0 否 从文件指定位置(字节偏移量)开始读取
length number — 否 指定读取的字节数;不传则读取到文件末尾
success function — 否 成功回调
fail function — 否 失败回调
complete function — 否 完成回调(成功或失败均触发)

encoding 合法值

值 说明
ascii
base64
binary
hex
ucs2 / ucs-2 / utf16le / utf-16le
utf8 / utf-8
latin1

success 回调参数

属性 类型 说明
data string | ArrayBuffer 文件内容;传入 encoding 时为 string,否则为 ArrayBuffer

# 返回值

无返回值。

# 示例代码

const fs = mt.getFileSystemManager()
// 读取文本文件
fs.readFile({
  filePath: `${mt.env.USER_DATA_PATH}/config.json`,
  encoding: 'utf8',
  success(res) {
    const config = JSON.parse(res.data)
    console.log(config)
  },
  fail(err) {
    console.error(err.errMsg)
  }
})

// 读取二进制文件
fs.readFile({
  filePath: `${mt.env.USER_DATA_PATH}/data.bin`,
  success(res) {
    console.log(res.data) // ArrayBuffer
  }
})

# 与微信差异

无差异(行为与微信小游戏一致)。

上次更新: 8/10/2026, 5:36:37 PM