Skip to content

Instantly share code, notes, and snippets.

@RahulBRB
Created October 17, 2023 13:18
Show Gist options
  • Save RahulBRB/5389917e7b071149639ca9548fcb1ce5 to your computer and use it in GitHub Desktop.
Save RahulBRB/5389917e7b071149639ca9548fcb1ce5 to your computer and use it in GitHub Desktop.
File system model for write, append, read, rename, unlink (delete). Commonly used features of the fs module include fs. readFile to read data from a file, fs. writeFile to write data to a file and replace the file if it already exists
let fs = require('fs')
//write file
fs.writeFile('myFile.txt','NodeJs code in code sample', function(){
console.log('File Create')
})
//append file
fs.appendFile('myCode.txt','My code append \n',function(){
console.log('File Appended')
})
//readFile
fs.readFile('myCode.txt','utf-8',function(err,data){
if(err) throw err;
console.log(data)
})
//renameFile
fs.rename('myCode.txt','myText.txt',function(err){
if(err) throw err;
console.log('File Renamed')
})
//unlink or Delete file
fs.unlink('myText.txt',function(err){
if(err) throw err;
console.log('File Deleted')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment