Description of the fs. createReadStream method in node. js, createreadstream
Method description:
Returns a readStream (object reading stream, input stream) object. (Readable Stream)
Syntax:
Copy codeThe 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:
Copy codeThe 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.
Copy codeThe Code is as follows:
Fs.createReadStream('sample.txt ', {start: 90, end: 99 });
Source code:
Copy codeThe Code is as follows:
Fs. createReadStream = function (path, options ){
Return new ReadStream (path, options );
};