fs.createWriteStream(path[, options])
options
还可以包括 start
选项,以允许在文件开头的某个位置写入数据,允许的值在 [0, Number.MAX_SAFE_INTEGER
] 范围内。
修改文件而不是替换它可能需要将 flags
选项设置为 r+
而不是默认的 w
。
encoding
可以是 <Buffer> 接受的任何一种。
如果将 autoClose
设置为 true(默认行为),则在 'error'
或 'finish'
时文件描述符将自动关闭。
如果 autoClose
为 false,则即使出现错误,文件描述符也不会关闭。
关闭它并确保没有文件描述符泄漏是应用程序的责任。
默认情况下,流将在销毁后触发 'close'
事件,就像大多数 Writable
流一样。
将 emitClose
选项设置为 false
以更改此行为。
通过提供 fs
选项,可以覆盖 open
、write
、writev
和 close
的相应 fs
实现。
在没有 writev()
的情况下覆盖 write()
会降低性能,因为某些优化 (_writev()
) 将被禁用。
提供 fs
选项时,需要覆盖 open
、close
、以及 write
和 writev
中的至少一个。
与 <fs.ReadStream> 一样,如果指定了 fd
,则 <fs.WriteStream> 将忽略 path
参数并使用指定的文件描述符。
这意味着不会触发 'open'
事件。
fd
应该是阻塞的;非阻塞 fd
应该传给 <net.Socket>。
如果 options
是字符串,则它指定编码。
path
<string> | <Buffer> | <URL>options
<string> | <Object>- Returns: <fs.WriteStream> See Writable Stream.
options
may also include a start
option to allow writing data at some
position past the beginning of the file, allowed values are in the
[0, Number.MAX_SAFE_INTEGER
] range. Modifying a file rather than replacing
it may require the flags
option to be set to r+
rather than the default w
.
The encoding
can be any one of those accepted by <Buffer>.
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.
By default, the stream will emit a 'close'
event after it has been
destroyed, like most Writable
streams. Set the emitClose
option to
false
to change this behavior.
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 open
,
close
, and at least one of write
and writev
are required.
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 fd
s
should be passed to <net.Socket>.
If options
is a string, then it specifies the encoding.