node.js - `fs.createReadStream` 是否将默认结束值设置为文件的当前结尾?

标签 node.js fs

我正在读取一个正在写入的文件。所以我有:

const readStream = fs.createReadStream(file, {start:0});

根据我的测试,看起来 fs.createReadStream 会为 {end:number} 设置一个值,其中读取流的结尾是文件的当前结尾。然而理论上,当流从文件中读取数据时,它可以一直读取到文件的末尾(因为在读取过程中正在写入新数据)。

有谁确切知道这是如何实现的?

最佳答案

您可以使用 autoClose: false 来防止自动关闭:

https://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options

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. If autoClose is set to true (default behavior), on 'error' or 'end' the file descriptor will be closed automatically.

关于node.js - `fs.createReadStream` 是否将默认结束值设置为文件的当前结尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57064325/

相关文章:

node.js - 如何在异步写入文件时锁定文件

javascript - fs.stats 不提供文件所需的状态

javascript - Node 版本 8 中 Async/Await 的性能指南

javascript - 在另一个文件中访问 MySQL 的输出

node.js - 如何在响应中获取更新的文档

javascript - 带有 express 和 react 的 Node.js,fs.readdir 找不到文件

node.js - 使用 Nodejs (fs) 访问通过输入 type=file 选择的文件

javascript - Firebase云功能1.0,当前用户没有身份验证信息

node.js - 根据条件从 Mongo 中过期/删除文档

node.js - 错误 : ENOENT: no such file or directory, 取消链接