string|ArrayBuffer FileSystemManager.readFileSync(string filePath, string encoding, number position, number length)
功能描述
FileSystemManager.readFile的同步版本。
参数
string filePath
要读取的文件的路径 (本地路径)
string encoding
指定读取文件的字符编码,如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容
encoding 的合法值
合法值 | 说明 |
---|---|
ascii | |
utf-8 | |
utf8 | |
base64 |
number position
从文件指定位置开始读,如果不指定,则从文件头开始读。读取的范围应该是左闭右开区间 [position, position+length)。有效范围:[0, fileLength - 1]。单位:byte
number length
指定文件的长度,如果不指定,则读到文件末尾。有效范围:[1, fileLength]。单位:byte
示例代码
const fs = mgtv.getFileSystemManager()
// 同步接口
try {
const res = fs.readFileSync(`${mgtv.env.USER_DATA_PATH}/hello.txt`, 'utf8', 0)
console.log(res)
} catch(e) {
console.error(e)
}