writable.end([chunk[, encoding]][, callback])


  • chunk <string> | <Buffer> | <Uint8Array> | <any> 可选的要写入的数据。 对于不在对象模式下操作的流,chunk 必须是字符串、BufferUint8Array。 对于对象模式的流,chunk 可以是除 null 之外的任何 JavaScript 值。
  • encoding <string> chunk 为字符串时的编码
  • callback <Function> 当流完成或错误时的可选回调
  • 返回: <this>

调用 writable.end() 方法表示不再有数据写入 Writable。 可选的 chunkencoding 参数允许在关闭流之前立即写入最后一个额外的数据块。 如果提供,则可选的 callback 函数被绑定为 'finish''error' 事件的监听器。

在调用 stream.end() 之后调用 stream.write() 方法将引发错误。

// 写入 'hello, ' 然后以 'world!' 结尾。
const fs = require('fs');
const file = fs.createWriteStream('example.txt');
file.write('hello, ');
file.end('world!');
// 现在不允许写入更多!
  • chunk <string> | <Buffer> | <Uint8Array> | <any> Optional data to write. For streams not operating in object mode, chunk must be a string, Buffer or Uint8Array. For object mode streams, chunk may be any JavaScript value other than null.
  • encoding <string> The encoding if chunk is a string
  • callback <Function> Optional callback for when the stream finishes or errors
  • Returns: <this>

Calling the writable.end() method signals that no more data will be written to the Writable. The optional chunk and encoding arguments allow one final additional chunk of data to be written immediately before closing the stream. If provided, the optional callback function is attached as a listener for the 'finish' and the 'error' event.

Calling the stream.write() method after calling stream.end() will raise an error.

// Write 'hello, ' and then end with 'world!'.
const fs = require('fs');
const file = fs.createWriteStream('example.txt');
file.write('hello, ');
file.end('world!');
// Writing more now is not allowed!