fs.createReadStream(path[, options])


与可读流的 16 kb 默认 highWaterMark 不同,此方法返回的流具有 64 kb 的默认 highWaterMark

options 可以包括 startend 值,以从文件中读取一定范围的字节而不是读取整个文件。 startend 都包含在内并从 0 开始计数。 如果指定了 fd 并且 start 被省略或未定义,则 fs.createReadStream() 从当前文件位置开始顺序读取。 encoding 可以是 Buffer 接受的任何一种字符编码。

如果指定了 fdReadStream 将忽略 path 参数并使用指定的文件描述符。 这意味着不会触发 'open' 事件。 fd 必须是阻塞的,非阻塞的 fd 应该传给 net.Socket

如果 fd 指向仅支持阻塞读取的字符设备(例如键盘或声卡),则在数据可用之前,读取操作不会完成。 这可以防止进程退出并且流自然关闭。

const fs = require('fs');
// 从某字符设备创建一个流。
const stream = fs.createReadStream('/dev/input/event0');
setTimeout(() => {
  stream.close(); // 这可能不会关闭流。
  // 人工标记流末尾,就好像底层资源本身已指示文件结尾一样,允许流关闭。
  // 这不会取消挂起的读取操作,如果存在此类操作,则该过程可能仍无法成功退出,直到完成为止。
  stream.push(null);
  stream.read(0);
}, 100);

如果 autoClosefalse,则即使出现错误,也不会关闭文件描述符。 应用程序负责关闭它并确保没有文件描述符泄漏。 如果 autoClose 设为 true(默认行为),则在 errorend 事件时将自动关闭文件描述符。

mode 用于设置文件模式(权限和粘滞位),但仅限于创建文件的情况。

例子,从一个大小为 100 字节的文件中读取最后 10 个字节:

fs.createReadStream('sample.txt', { start: 90, end: 99 });

如果 options 是字符串,则它指定字符编码。

Unlike the 16 kb default highWaterMark for a readable stream, the stream returned by this method has a default highWaterMark of 64 kb.

options can include start and end values to read a range of bytes from the file instead of the entire file. Both start and end are inclusive and start counting at 0. If fd is specified and start is omitted or undefined, fs.createReadStream() reads sequentially from the current file position. The encoding can be any one of those accepted by Buffer.

If fd is specified, ReadStream 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.

If fd points to a character device that only supports blocking reads (such as keyboard or sound card), read operations do not finish until data is available. This can prevent the process from exiting and the stream from closing naturally.

const fs = require('fs');
// Create a stream from some character  device.
const stream = fs.createReadStream('/dev/input/event0');
setTimeout(() => {
  stream.close(); // This may not close the stream.
  // Artificially marking end-of-stream, as if the underlying resource had
  // indicated end-of-file by itself, allows the stream to close.
  // This does not cancel pending read operations, and if there is such an
  // operation, the process may still not be able to exit successfully
  // until it finishes.
  stream.push(null);
  stream.read(0);
}, 100);

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.

mode sets the file mode (permission and sticky bits), but only if the file was created.

An example to read the last 10 bytes of a file which is 100 bytes long:

fs.createReadStream('sample.txt', { start: 90, end: 99 });

If options is a string, then it specifies the encoding.