In this session, I am going to explain about fs module to read the file and write content to a file. And their code to memorize to use them for your future endeavors.
Using fs to read both synchronously and asynchronously :
most for the time we asynchronous code to write in rare cases you will use synchrounous.
code for asynchronous for reading file and writing file:
fs.readFile(filepath,(err,data)=>{
if(err){
throw err;
}else{
console.log(data.toString())
}
})
_______________________________
fs.writeFile(filepath,str,(err,data)=>{
if(err){
throw err;
}
})
code for synchronous writing and reading file:
fs.writeFileSync(filePath,str,”utf8”);
__________________________________
let t=fs.readFileSync(filePath,”utf8”);
console.log(t)