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.
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.
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('*********************'); });
No comments:
Post a Comment