filehandle.write(buffer[, offset[, length[, position]]])
buffer<Buffer> | <TypedArray> | <DataView> | <string> | <Object>offset<integer> 要开始写入数据的buffer的起始位置。 默认值:0length<integer> 要从buffer写入的字节数。 默认值:buffer.byteLengthposition<integer> 要写入来自buffer的数据的文件的开头偏移量。 如果position不是number,则数据将被写入当前位置。 有关更多详细信息,请参阅 POSIXpwrite(2)文档。- 返回: <Promise>
将 buffer 写入文件。
如果 buffer 是普通的对象,则它必须具有自有的(不是继承的)toString 函数属性。
使用包含以下两个属性的对象来解决 promise:
bytesWritten<integer> 写入的字节数buffer<Buffer> | <TypedArray> | <DataView> | <string> | <Object> 对被写入的buffer的引用。
在同一文件上多次使用 filehandle.write() 而不等待 promise 被解决(或拒绝)是不安全的。
对于这种情况,请使用 fs.createWriteStream()。
在 Linux 上,以追加模式打开文件时,位置写入不起作用。 内核会忽略位置参数,并始终将数据追加到文件末尾。
buffer<Buffer> | <TypedArray> | <DataView> | <string> | <Object>offset<integer> The start position from withinbufferwhere the data to write begins. Default:0length<integer> The number of bytes frombufferto write. Default:buffer.byteLengthposition<integer> The offset from the beginning of the file where the data frombuffershould be written. Ifpositionis not anumber, the data will be written at the current position. See the POSIXpwrite(2)documentation for more detail.- Returns: <Promise>
Write buffer to the file.
If buffer is a plain object, it must have an own (not inherited) toString
function property.
The promise is resolved with an object containing two properties:
bytesWritten<integer> the number of bytes writtenbuffer<Buffer> | <TypedArray> | <DataView> | <string> | <Object> a reference to thebufferwritten.
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 fs.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.