fs.createWriteStream(path[,
options])
This
method creates a writeable stream, used to write data to a file.
Below
table summarizes the arguments of createWriteStream method.
Argument
|
Type
|
Description
|
|||||||||||||||||||||
path
|
string
or Buffer or URL
|
File
path to write data.
|
|||||||||||||||||||||
options
|
string
or Object
|
Below
table summarizes the options supported by createWriteStream method.
|
Find
he below working application.
var fs = require('fs'); var writeStream = fs.createWriteStream('data.txt'); writeStream.on('finish', () => { console.log('*********************'); console.log('All writes are now complete.'); console.log('*********************\n'); }); writeStream.on('close', () => { console.log('*********************'); console.log('Closing the stream'); console.log('*********************'); }); writeStream.write('Hello, How are you\n'); writeStream.write('I am fine, thank you....\n'); writeStream.end('This is the end of the file'); writeStream.close();
Output
*********************
All
writes are now complete.
*********************
*********************
Closing
the stream
*********************
Open
‘data.txt’, you can see below messages in the console.
Hello,
How are you
I
am fine, thank you....
This
is the end of the file
No comments:
Post a Comment