fs.createWriteStream(path[, options])
path<string> | <Buffer> | <URL>options<string> | <Object>flags<string> 参见 文件系统flags支持。默认值:'w'。encoding<string> 默认值:'utf8'fd<integer> | <FileHandle> 默认值:nullmode<integer> 默认值:0o666autoClose<boolean> 默认值:trueemitClose<boolean> 默认值:truestart<integer>fs<Object> | <null> 默认值:nullsignal<AbortSignal> | <null> 默认值:nullhighWaterMark<number> 默认值:16384flush<boolean> 如果为true,在关闭底层文件描述符之前会先刷新它。默认值:false。
- 返回:<fs.WriteStream>
options 也可能包含 start 选项,允许在某些地方写入数据
文件开头之后的位置,允许的值在
[0, Number.MAX_SAFE_INTEGER] 范围。修改文件而不是
替换它可能需要将 flags 选项设置为 r+,而不是
默认的 W。encoding 可以是<Buffer>接受的任何一种。
如果在 'error' 或 'finish' 上将 autoClose 设置为 true(默认行为),文件描述符将会自动关闭。如果 autoClose 为 false,即使发生错误,文件描述符也不会关闭。由应用负责关闭它,并确保不会发生文件描述符泄漏。
【If autoClose is set to true (default behavior) on 'error' or 'finish'
the file descriptor will be closed automatically. If autoClose is false,
then the file descriptor won't be closed, even if there's an error.
It is the application's responsibility to close it and make sure there's no
file descriptor leak.】
默认情况下,流在被销毁后会触发 'close' 事件。将 emitClose 选项设置为 false 可以更改此行为。
【By default, the stream will emit a 'close' event after it has been
destroyed. Set the emitClose option to false to change this behavior.】
通过提供 fs 选项,可以覆盖 open、write、writev 和 close 对应的 fs 实现。仅覆盖 write() 而不覆盖 writev() 可能会降低性能,因为某些优化(_writev())将被禁用。当提供 fs 选项时,至少需要覆盖 write 和 writev 其中之一。如果未提供 fd 选项,还需要覆盖 open。如果 autoClose 为 true,还需要覆盖 close。
【By providing the fs option it is possible to override the corresponding fs
implementations for open, write, writev, and close. Overriding write()
without writev() can reduce performance as some optimizations (_writev())
will be disabled. When providing the fs option, overrides for at least one of
write and writev are required. If no fd option is supplied, an override
for open is also required. If autoClose is true, an override for close
is also required.】
像 <fs.ReadStream> 一样,如果指定了 fd,<fs.WriteStream> 将会忽略 path 参数,并使用指定的文件描述符。这意味着不会触发 'open' 事件。fd 应该是阻塞的;非阻塞的 fd 应该传给 <net.Socket>。
【Like <fs.ReadStream>, if fd is specified, <fs.WriteStream> will ignore the
path argument and will use the specified file descriptor. This means that no
'open' event will be emitted. fd should be blocking; non-blocking fds
should be passed to <net.Socket>.】
如果 options 是字符串,那么它指定了编码。
【If options is a string, then it specifies the encoding.】