Friday 19 October 2018

node.js: fs.createReadStream(path[, options]): Create readable stream

fs.createReadStream(path[, options])
This method creates a readable stream. In my previous posts, I explained how to read a file using readFile and readFileSync methods. One of the disadvantage of these methods is, these methods read the entire content of the file to in-memory. In case of big files, your application will endup in lot of memory consumption. To resolve the memory issues, you can create a readable stream to the file and read chuck by chucnk.

Below table summarizes the arguments of createReadStream method.

Argument
Type
Description
path
string or Buffer or URL
Specifies the path of the file.
options
string or Object.
Below table summarizes different options supported by createReadStream method.

Option
Type
Description
flags
string
Default value is r.
encoding
string
Default value is null
fd
integer
Default value is null.

If fd is specified and start is omitted or undefined, fs.createReadStream() reads sequentially from the current file position.

If fd is specified, ReadStream will ignore the path argument and will use the specified file descriptor.

mode
integer
Default value is 0o666
autoclose
boolean
Default value is true. If it is set true, then the stream is closed. If it is set to false, then it is application responsibility to close the stream.
start
integer
Start byte of the stream to read. By using ‘start’ and ‘end’ bytes, you can read specific portion of the file.

Example
fs.createReadStream('sample.txt', { start: 10, end: 29 });
Above statement read 30 (from 10th byte to 29th byte) bytes from the file.
end
integer
End byte of the stream.

Example
fs.createReadStream('sample.txt', { start: 10, end: 29 });
Above statement read 30 (from 10th byte to 29th byte) bytes from the file.
highWaterMark
integer
Specifies the chuck size. Default value is 64 * 1024 (64kb).

A

Node.js support below flags.
a.   'a' - Open file for appending. The file is created if it does not exist.
b.   'ax' - Like 'a' but fails if the path exists.
c.   'a+' - Open file for reading and appending. The file is created if it does not exist.
d.   'ax+' - Like 'a+' but fails if the path exists.
e.   'as' - Open file for appending in synchronous mode. The file is created if it does not exist.
f.    'as+' - Open file for reading and appending in synchronous mode. The file is created if it does not exist.
g.   'r' - Open file for reading. An exception occurs if the file does not exist.
h.   'r+' - Open file for reading and writing. An exception occurs if the file does not exist.
i.     'rs+' - Open file for reading and writing in synchronous mode. Instructs the operating system to bypass the local file system cache.
j.    'w' - Open file for writing. The file is created (if it does not exist) or truncated (if it exists).
k.   'wx' - Like 'w' but fails if the path exists.
l.     'w+' - Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).
m. 'wx+' - Like 'w+' but fails if the path exists.

Below table summarizes the modes supported by node.js
Constant (mode)
Octal value
Description
fs.constants.S_IRUSR
0o400
read by owner
fs.constants.S_IWUSR
0o200
write by owner
fs.constants.S_IXUSR
0o100
execute/search by owner
fs.constants.S_IRGRP        
0o40
read by group
fs.constants.S_IWGRP
0o20
write by group
fs.constants.S_IXGRP
0o10
execute/search by group
fs.constants.S_IROTH
0o4
read by others
fs.constants.S_IWOTH
0o2
write by others
fs.constants.S_IXOTH
0o1
execute/search by others

Find the below working application.

HelloWorld.js
var fs = require('fs');

var readStream = fs.createReadStream('data.txt', 'UTF-8');

readStream.once('data', (chunk) => {
    console.log('Started reading the content\n');
});

readStream.on('data', (chunk) => {
    console.log('*********************');
    console.log(`Reading ${chunk.length} bytes`);
    //console.log(chunk); // Uncomment this line to print tghe data
    console.log('*********************\n');
});

readStream.on('end', () => {
    console.log('*********************');
    console.log('Finished reading');
    console.log('*********************\n');
});

readStream.on('close', () => {
    console.log('*********************');
    console.log('Closing the stream');
    console.log('*********************');
});



Previous                                                 Next                                                 Home

No comments:

Post a Comment