📌 从基础库 3.125.4 版本开始支持
FileSystemManager.readFile 的同步版本
参数 | 类型 | 说明 |
|---|---|---|
| filePath | string | 要读取的文件的路径 (本地路径) |
| encoding | string | 指定读取文件的字符编码(小红书仅支持:utf-8/utf8/binary),不传则返回 ArrayBuffer |
| position | number | 从文件指定位置开始读(单位:byte),有效范围:0, fileLength - 1 |
| length | number | 指定读取长度(单位:byte),有效范围:1, fileLength |
值 | 说明 |
|---|---|
| utf-8 | UTF-8 编码 |
| utf8 | UTF-8 编码 |
| binary | 二进制 |
const fs = xhs.getFileSystemManager()
try {
// 读取UTF-8文本
const text = fs.readFileSync(
`${xhs.env.USER_DATA_PATH}/hello.txt`,
'utf8',
0
)
console.log(text)
// 读取二进制数据
const buffer = fs.readFileSync(
`${xhs.env.USER_DATA_PATH}/image.png`,
'binary'
)
console.log(buffer)
} catch(e) {
console.error('读取文件失败:', e)
}