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


  • chunk <string> | <Buffer> | <Uint8Array> | <any> 要写入的数据。 对于非对象模式的流chunk 必须是字符串、Buffer、或 Uint8Array。 对于对象模式的流, chunk 可以是任何 JavaScript 值,除了 null
  • encoding <string> 如果 chunk 是字符串,则指定字符编码。
  • callback <Function> 当流结束时的回调函数。
  • 返回: <this>

调用 writable.end() 表明已没有数据要被写入可写流。 可选的 chunkencoding 参数可以在关闭流之前再写入一块数据。 如果传入了 callback 函数,则会做为监听器添加到 'finish' 事件。

调用 stream.end() 之后再调用 stream.write() 会导致错误。

// 先写入 'hello, ',结束前再写入 'world!'。
const fs = require('fs');
const file = fs.createWriteStream('例子.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 is finished
  • 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' 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!