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 写入文件。

使用包含以下两个属性的对象来解决 promise:

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

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

  • buffer <Buffer> | <TypedArray> | <DataView>
  • offset <integer> The start position from within buffer where the data to write begins.
  • length <integer> The number of bytes from buffer to write. Default: buffer.byteLength - offset
  • position <integer> | <null> The offset from the beginning of the file where the data from buffer should be written. If position is not a number, the data will be written at the current position. See the POSIX pwrite(2) documentation for more detail. Default: null
  • Returns: <Promise>

Write buffer to the file.

The promise is resolved with an object containing two properties:

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

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.