This article mainly introduces node. fs. the createReadStream method is described in this article. createReadStream method description, syntax, receiving parameters, use instances and implementation source code. For more information, see
Method description:
Returns a readStream (object reading stream, input stream) object. (Readable stream)
Syntax:
The code is as follows:
Fs. createReadStream (path, [options])
Because this method belongs to the fs module, we need to introduce the fs module (var fs = require ("fs") before use "))
Receiving parameters:
Path: (string) path of the file to be read
Options: (object) an array object contains the following attributes:
The code is as follows:
{Flags: 'R ',
Encoding: null,
Fd: null,
Mode: 0666,
AutoClose: true
}
Options can use start and end to set the range of bytes that the file can read, rather than reading the entire file.
If both start and end are included, it starts from 0.
Encodeing can be 'utf8', 'ascii ', or 'base64.
If autoClose is false, the file descriptor will not be closed even if they report an error.
It is best to close it and ensure that there is no file descriptor leakage.
If autoClose is set to true (the default action), the file descriptor of the error or end is automatically disabled.
Example:
In this example, the last 10 10 bytes in a KB file are read.
The code is as follows:
Fs.createReadStream('sample.txt ', {start: 90, end: 99 });
Source code:
The code is as follows:
Fs. createReadStream = function (path, options ){
Return new ReadStream (path, options );
};