filehandle.write(buffer, offset[, length[, position]])


  • buffer <Buffer> | <TypedArray> | <DataView>
  • offset <integer> 数据开始写入时在 buffer 中的起始位置。
  • length <integer>buffer 开始写入的字节数。默认值: buffer.byteLength - offset
  • position <integer> | <null> 从文件开头开始偏移的位置,buffer 中的数据应写入该位置。如果 position 不是 number 类型,数据将写入当前的位置。详情参见 POSIX pwrite(2) 文档。默认值: null
  • 返回:<Promise>

buffer 写入文件。

🌐 Write buffer to the file.

这个 promise 是通过一个包含两个属性的对象来实现的:

🌐 The promise is fulfilled with an object containing two properties:

在同一个文件上多次使用 filehandle.write() 是不安全的,如果不等待 Promise 被解决(或被拒绝)。对于这种情况,请使用 filehandle.createWriteStream()

🌐 It is unsafe to use filehandle.write() multiple times on the same file without waiting for the promise to be fulfilled (or rejected). For this scenario, use filehandle.createWriteStream().

在 Linux 上,当文件以追加模式打开时,位置写入不起作用。内核会忽略位置参数,并始终将数据追加到文件末尾。

🌐 On Linux, positional writes do not work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.