NodeJS File System Module

📔 : NodeJS 🔗

Node.js में files को handle करने के लिए fs module का use किया जाता है। fs module में कई सारे predefined functions हैं , जिनकी help से file को read , write , update , delete कर सकते हैं। इस topic में आप सीखेंगे कि कैसे fs module का use करके files को handle करते हैं।

NodeJS Read File

file को read करने के लिए fs module का readFile() method का use किया जाता है , जो 3 parameters accept करता है। पहला filename जिसे read करना है , दूसरा options (utf & other) मतलब किस type का content read करना है और तीसरा callback function. नीचे example में देखकर आप समझ सकते हैं की इसे कैसे use करते हैं।

NodeJS readFile() Example

मेरे पास एक simple fsdemo.txt file है जिसमे कुछ ऐसा content है , इस फाइल का use करके हम different- different file operations perform करेंगे।

file : demo.txt
hello !
this is a simple text file for
testinf purpose.....

File : app.js

Copy Fullscreen Close FullscreenRun
// firt load fs module.
const fs = require('fs');
const filename = 'demo.txt';
fs.readFile(filename, 'utf8', function(err, data) {
    // check if error occured.
    if(err)
        throw err;
    
    console.log('File data : \n',data);
});
C:\Users\HP\Desktop\workspace\nodejs>node app.js
File data :
 hello !
this is a simple text file for
testinf purpose.....

NodeJS Update / Create File

File को update और create करने के लिए दो methods का use किया जाता है

  1. appendFile()
  2. writeFile()

NodeJS appendFile()

appendFile() method का use file को create और update करने के लिए किया जाता है। अगर targeted file मिल गयी तो new content file के last में ही append होता है। और अगर targeted file नहीं मिली तो new file create करके pass किया गया content insert हो जाता है।
See Example :

File : app.js

Copy Fullscreen Close FullscreenRun
const fs = require('fs');
const filename = 'new_file.txt';
fs.appendFile(filename, 'Updated content..', function(err) {
   if(err)
      throw err;

   console.log('Content updated in file :'+ filename);
});
C:\Users\HP\Desktop\workspace\nodejs>node app.js
Content updated in file :new_file.txt

ऊपर दिए गए example को आप दो बार run करके देखना , पहली बार में file create हो जायगी और दूसरी बार run करने पर file create न होकर सिर्फ उसका content update हो जायगा।

NodeJS writeFile()

writeFile() method का use file को create और किसी exist file में content replace करने के लिए किया जाता है। अगर targeted file मिल गयी तो new content file के पुराने content को replace कर देगा। और अगर targeted file नहीं मिली तो new file create करके pass किया गया content insert हो जाता है।
See Exmaple :

File : app.js

Copy Fullscreen Close FullscreenRun
const fs = require('fs');
const filename = 'write_file.txt';
fs.writeFile(filename, 'Updated content..', function(err) {
   if(err)
      throw err;

   console.log('Content replaced in file :'+ filename);
});
C:\Users\HP\Desktop\workspace\nodejs>node app.js
Content replaced in file :write_file.txt

ऊपर दिए गए example को भी आप दो बार run करके देखना , पहली बार में file create हो जायगी और दूसरी बार run करने पर file create होकर उसका content replace हो जायगा।

NodeJS Delete File

file को delete करने के लिए unlink() method का use किया जाता है।
Example :

File : app.js

Copy Fullscreen Close FullscreenRun
const fs = require('fs');
const filename = 'write_file.txt';
fs.unlink(filename, function(err) {
   if(err)
      throw err;

   console.log(filename+' deleted successfully !');
});
C:\Users\HP\Desktop\workspace\nodejs>node app.js
write_file.txt deleted successfully !

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers