Skip to content
On this page

FileSystemManager.writeSync

FileSystemManager.write 的同步方法

参数

参数类型默认值必填说明
fdstring文件描述符。通过 FileSystemManager.open 接口获得
datastring | ArrayBuffer写入的内容,类型为 string 或 ArrayBuffer
offsetnumber0只在 data 类型是 ArrayBuffer 时有效,决定 arrayBuffe 中要被写入的部位,即 arrayBuffer 中的索引,默认0
lengthnumber只在 data 类型是 ArrayBuffer 时有效,指定要写入的字节数,默认为 arrayBuffer 从0开始偏移 offset 个字节后剩余的字节数
encodingstringutf8只在 data 类型是 String 时有效,指定写入文件的字符编码,默认为 utf8
合法值说明
ascii
base64
binary
hex
ucs2以小端序读取
ucs-2以小端序读取
utf16le以小端序读取
utf-16le以小端序读取
utf-8
utf8
latin1
positionnumber指定文件开头的偏移量,即数据要被写入的位置。当 position 不传或者传入非 Number 类型的值时,数据会被写入当前指针所在位置

返回值

WriteResult

文件读取结果

Example

ts
const fs = ek.getFileSystemManager()
try {
  const fd = fs.openSync({ filePath: `${ek.env.USER_DATA_PATH}/hello.txt`, flag: 'a+' })
  const res = fs.writeSync({
    fd,
    data: 'some text'
  })
  console.log(res.bytesWritten)
} catch (e) {
  console.log(e)
}