Node.js fs Write text to stream with UTF8 encoding

Description

Node.js fs Write text to stream with UTF8 encoding


var fs = require("fs");
var data = 'Simply Easy Learning \nNode.js';

// Create a writable stream
var writerStream = fs.createWriteStream('output.txt');

// Write the data to stream with encoding to be utf8
writerStream.write(data,'UTF8');

// Mark the end of file
writerStream.end();/*w  w  w . j a va2  s  . co m*/

// Handle stream events --> finish, and error
writerStream.on('finish', function() {
    console.log("Write completed.");
});

writerStream.on('error', function(err){
    console.log(err.stack);
});

console.log("Program Ended");
console.log( __filename );



PreviousNext

Related