Home  >  Article  >  Web Front-end  >  Instructions for using the fs.createReadStream method in node.js_node.js

Instructions for using the fs.createReadStream method in node.js_node.js

WBOY
WBOYOriginal
2016-05-16 16:26:001502browse

Method description:

Returns a readStream (file reading stream, input stream) object. (readable stream)

Grammar:

Copy code The code is as follows:

fs.createReadStream(path, [options])

Since this method belongs to the fs module, the fs module needs to be introduced before use (var fs= require(“fs”) )

Receive parameters:

path: (string) The path of the file to be read

options: (object) The array object contains the following properties

Copy code The code is as follows:

{ flags: 'r',
encoding: null,
fd: null,
Mode: 0666,
autoClose: true
}

options can set the range of bytes that the file can read through start and end, instead of reading the entire file.

If both start and end are included, it will start from 0.

Encoding can be in three formats: ‘utf8′, ‘ascii’, or ‘base64′.

If autoClose is false, file descriptors will not be closed, even if they report an error.

Better to turn it off and make sure there are no file descriptor leaks.

If autoClose is true (the default behavior), the file descriptor on error or end will be automatically closed.

Example:

This example will read the last 10 sections of a 100k file.

Copy code The code is as follows:

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

Source code:

Copy code The code is as follows:

fs.createReadStream = function(path, options) {
Return new ReadStream(path, options);
};
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn