Method description:
Returns a readStream (file read stream, input stream) object. (Readable stream)
grammar:
The code copy is as follows:
fs.createReadStream(path, [options])
Since this method belongs to the FS module, it is necessary to introduce the FS module before use (var fs= require("fs") )
Receive parameters:
path: (string) file path to read
options: (object) The array object contains the following properties
The code copy is as follows:
{ flags: 'r',
encoding: null,
fd: null,
mode: 0666,
autoClose: true
}
options can set the range of bytes that a file can read by start and end instead of reading the entire file.
If both start and end are included, it will start from 0.
encodeing can be in three formats: 'utf8', 'ascii', or 'base64'.
If autoClose is false, the file descriptor will not be closed, even if they report an error.
It's best to turn it off and make sure that there is no file descriptor leak.
If autoClose is true (the default behavior), the file descriptor for errors or ends will be automatically closed.
example:
This example will read the last 10 bytes of content in a 100k file.
The code copy is as follows:
fs.createReadStream('sample.txt', {start: 90, end: 99});
Source code:
The code copy is as follows:
fs.createReadStream = function(path, options) {
return new ReadStream(path, options);
};