fs.write(fd, buffer, offset[, length[, position]], callback)


buffer 写入由 fd 指定的文件。

【Write buffer to the file specified by fd.】

offset 决定要写入缓冲区的部分,而 length 是指定要写入字节数的整数。

position 指的是应将此数据写入文件起始位置的偏移量。如果 typeof position !== 'number',数据将写入当前位置。参见 pwrite(2)

回调函数将接收三个参数 (err, bytesWritten, buffer),其中 bytesWritten 指定了从 buffer 写入了多少字节。

【The callback will be given three arguments (err, bytesWritten, buffer) where bytesWritten specifies how many bytes were written from buffer.】

如果以其 util.promisify()ed 版本调用此方法,它会返回一个包含 bytesWrittenbuffer 属性的 Object 的 Promise。

【If this method is invoked as its util.promisify()ed version, it returns a promise for an Object with bytesWritten and buffer properties.】

在同一个文件上多次使用 fs.write() 而不等待回调是不安全的。对于这种情况,建议使用 fs.createWriteStream()

【It is unsafe to use fs.write() multiple times on the same file without waiting for the callback. For this scenario, fs.createWriteStream() is recommended.】

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

【On Linux, positional writes don't 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.】