6 Program to read and write file in NodeJS

Synchronous: First it will read the file then execute the next statement.

Program:

var fs = require('fs');



var r = fs.readFileSync('demo.txt','utf8');



fs.writeFileSync('demo.txt', 'CODING ATHARVA')



console.log(r);



console.log('Hey There');


Demo.txt (Before):

Demo.txt

Demo.txt (After):

Demo.txt


Output:

Synchronous file read and write in NodeJS

ASynchronous: It will read file and simultaneously execute the next statements.

Program:

var fs = require('fs');





fs.readFile('demo.txt','utf8',function(err,data){

    console.log(data);

});



console.log('Hey There');


Demo.txt:
Demo.txt
Output:

ASynchronous file read in NodeJS

Previous
Next Post »