अध्याय 4: फाइल सिस्टम (File System)

अध्याय 4: फाइल सिस्टम (File System)

Node.js का फाइल सिस्टम मॉड्यूल (File System Module) आपको फाइलों और डायरेक्टरीज़ के साथ काम करने की सुविधा प्रदान करता है। यह मॉड्यूल फाइलों को पढ़ने, लिखने, अपडेट करने, और डिलीट करने के लिए विभिन्न प्रकार के फंक्शंस प्रदान करता है। इस अध्याय में, हम Node.js के फाइल सिस्टम मॉड्यूल का उपयोग करके फाइल सिस्टम ऑपरेशन्स को कैसे संचालित करें, इसे समझेंगे।

फाइल सिस्टम मॉड्यूल का उपयोग करना आसान है और यह आपको सिंक्रोनस और असिंक्रोनस दोनों प्रकार की ऑपरेशन्स करने की अनुमति देता है। इस अध्याय में, हम फाइलों को पढ़ने और लिखने के बुनियादी कार्यों से शुरू करेंगे और फिर डायरेक्टरीज़ के साथ काम करने की अधिक उन्नत तकनीकों पर जाएंगे।

फाइलों को पढ़ना और लिखना (Reading from and Writing to Files)

Node.js का फाइल सिस्टम मॉड्यूल (File System Module) आपको फाइलों को पढ़ने और लिखने की सुविधा प्रदान करता है। इस सेक्शन में, हम देखेंगे कि कैसे फाइलों को पढ़ा और लिखा जा सकता है। Node.js में फाइल सिस्टम से संबंधित अधिकांश ऑपरेशन्स असिंक्रोनस होते हैं, लेकिन यह सिंक्रोनस ऑपरेशन्स का समर्थन भी करता है। आइए इन दोनों तरीकों को समझें।

1. फाइल सिस्टम मॉड्यूल इंपोर्ट करना (Importing the File System Module)

सबसे पहले, आपको फाइल सिस्टम मॉड्यूल (fs) को इंपोर्ट करना होगा:
const fs = require('fs');

2. फाइल पढ़ना (Reading from a File)

फाइल पढ़ने के लिए, आप fs.readFile() फंक्शन का उपयोग कर सकते हैं। यह फंक्शन असिंक्रोनस तरीके से फाइल पढ़ता है और एक कॉलबैक फंक्शन के साथ परिणाम लौटाता है:

fs.readFile('example.txt', 'utf8', (err, data) => {
    if (err) {
        console.error('Error reading the file:', err);
        return;
    }
    console.log('File content:', data);
});

सिंक्रोनस तरीके से फाइल पढ़ने के लिए, आप fs.readFileSync() फंक्शन का उपयोग कर सकते हैं:

try {
    const data = fs.readFileSync('example.txt', 'utf8');
    console.log('File content:', data);
} catch (err) {
    console.error('Error reading the file:', err);
}

3. फाइल लिखना (Writing to a File)

फाइल में डेटा लिखने के लिए, आप fs.writeFile() फंक्शन का उपयोग कर सकते हैं। यह फंक्शन असिंक्रोनस तरीके से फाइल में डेटा लिखता है और एक कॉलबैक फंक्शन के साथ परिणाम लौटाता है:

const content = 'This is an example text.';

fs.writeFile('example.txt', content, 'utf8', (err) => {
    if (err) {
        console.error('Error writing to the file:', err);
        return;
    }
    console.log('File has been written successfully.');
});

सिंक्रोनस तरीके से फाइल में डेटा लिखने के लिए, आप fs.writeFileSync() फंक्शन का उपयोग कर सकते हैं:

const content = 'This is an example text.';

try {
    fs.writeFileSync('example.txt', content, 'utf8');
    console.log('File has been written successfully.');
} catch (err) {
    console.error('Error writing to the file:', err);
}

4. फाइल में डेटा जोड़ना (Appending Data to a File)

फाइल में डेटा जोड़ने के लिए, आप fs.appendFile() फंक्शन का उपयोग कर सकते हैं। यह फंक्शन असिंक्रोनस तरीके से फाइल में डेटा जोड़ता है:

const additionalContent = '\nThis is additional text.';

fs.appendFile('example.txt', additionalContent, 'utf8', (err) => {
    if (err) {
        console.error('Error appending to the file:', err);
        return;
    }
    console.log('Data has been appended successfully.');
});

सिंक्रोनस तरीके से फाइल में डेटा जोड़ने के लिए, आप fs.appendFileSync() फंक्शन का उपयोग कर सकते हैं:

const additionalContent = '\nThis is additional text.';

try {
    fs.appendFileSync('example.txt', additionalContent, 'utf8');
    console.log('Data has been appended successfully.');
} catch (err) {
    console.error('Error appending to the file:', err);
}

5. फाइल डिलीट करना (Deleting a File)

फाइल को डिलीट करने के लिए, आप fs.unlink() फंक्शन का उपयोग कर सकते हैं। यह फंक्शन असिंक्रोनस तरीके से फाइल को डिलीट करता है:

fs.unlink('example.txt', (err) => {
    if (err) {
        console.error('Error deleting the file:', err);
        return;
    }
    console.log('File has been deleted successfully.');
});

सिंक्रोनस तरीके से फाइल को डिलीट करने के लिए, आप fs.unlinkSync() फंक्शन का उपयोग कर सकते हैं:

try {
    fs.unlinkSync('example.txt');
    console.log('File has been deleted successfully.');
} catch (err) {
    console.error('Error deleting the file:', err);
}

Node.js का फाइल सिस्टम मॉड्यूल फाइलों के साथ काम करने के लिए आवश्यक सभी सुविधाएं प्रदान करता है। इन बुनियादी ऑपरेशन्स को समझकर, आप अपने एप्लिकेशंस में फाइलों को प्रभावी ढंग से प्रबंधित कर सकते हैं। अगले सेक्शन में, हम डायरेक्टरीज़ के साथ काम करने के लिए आवश्यक तकनीकों पर ध्यान देंगे।

डाइरेक्टरी के साथ काम करना (Working with Directories)

Node.js का फाइल सिस्टम मॉड्यूल न केवल फाइलों के साथ, बल्कि डायरेक्टरीज़ के साथ काम करने की भी सुविधा प्रदान करता है। इस सेक्शन में, हम देखेंगे कि कैसे आप डायरेक्टरीज़ को क्रिएट, रीड, अपडेट और डिलीट कर सकते हैं। आइए चरण-दर-चरण इन ऑपरेशन्स को समझें।

1. डायरेक्टरी क्रिएट करना (Creating a Directory)

डायरेक्टरी क्रिएट करने के लिए, आप fs.mkdir() फंक्शन का उपयोग कर सकते हैं। यह फंक्शन असिंक्रोनस तरीके से डायरेक्टरी क्रिएट करता है:

fs.mkdir('new-directory', (err) => {
    if (err) {
        console.error('Error creating directory:', err);
        return;
    }
    console.log('Directory created successfully.');
});

सिंक्रोनस तरीके से डायरेक्टरी क्रिएट करने के लिए, आप fs.mkdirSync() फंक्शन का उपयोग कर सकते हैं:

try {
    fs.mkdirSync('new-directory');
    console.log('Directory created successfully.');
} catch (err) {
    console.error('Error creating directory:', err);
}

2. डायरेक्टरी पढ़ना (Reading a Directory)

डायरेक्टरी की सामग्री को पढ़ने के लिए, आप fs.readdir() फंक्शन का उपयोग कर सकते हैं। यह फंक्शन असिंक्रोनस तरीके से डायरेक्टरी की सामग्री को पढ़ता है और एक कॉलबैक फंक्शन के साथ परिणाम लौटाता है:

fs.readdir('existing-directory', (err, files) => {
    if (err) {
        console.error('Error reading directory:', err);
        return;
    }
    console.log('Directory contents:', files);
});

सिंक्रोनस तरीके से डायरेक्टरी की सामग्री को पढ़ने के लिए, आप fs.readdirSync() फंक्शन का उपयोग कर सकते हैं:

try {
    const files = fs.readdirSync('existing-directory');
    console.log('Directory contents:', files);
} catch (err) {
    console.error('Error reading directory:', err);
}

3. डायरेक्टरी का नाम बदलना (Renaming a Directory)

डायरेक्टरी का नाम बदलने के लिए, आप fs.rename() फंक्शन का उपयोग कर सकते हैं। यह फंक्शन असिंक्रोनस तरीके से डायरेक्टरी का नाम बदलता है:

fs.rename('old-directory', 'new-directory', (err) => {
    if (err) {
        console.error('Error renaming directory:', err);
        return;
    }
    console.log('Directory renamed successfully.');
});

सिंक्रोनस तरीके से डायरेक्टरी का नाम बदलने के लिए, आप fs.renameSync() फंक्शन का उपयोग कर सकते हैं:

try {
    fs.renameSync('old-directory', 'new-directory');
    console.log('Directory renamed successfully.');
} catch (err) {
    console.error('Error renaming directory:', err);
}

4. डायरेक्टरी हटाना (Removing a Directory)

डायरेक्टरी को हटाने के लिए, आप fs.rmdir() फंक्शन का उपयोग कर सकते हैं। यह फंक्शन असिंक्रोनस तरीके से डायरेक्टरी को हटाता है:

fs.rmdir('directory-to-remove', (err) => {
    if (err) {
        console.error('Error removing directory:', err);
        return;
    }
    console.log('Directory removed successfully.');
});

सिंक्रोनस तरीके से डायरेक्टरी को हटाने के लिए, आप fs.rmdirSync() फंक्शन का उपयोग कर सकते हैं:

try {
    fs.rmdirSync('directory-to-remove');
    console.log('Directory removed successfully.');
} catch (err) {
    console.error('Error removing directory:', err);
}

5. रिकर्सिव डायरेक्टरी क्रिएशन (Recursive Directory Creation)

यदि आपको एक ही समय में कई नेस्टेड डायरेक्टरीज़ क्रिएट करनी हैं, तो आप fs.mkdir() या fs.mkdirSync() में { recursive: true } ऑप्शन का उपयोग कर सकते हैं:

fs.mkdir('parent-directory/child-directory', { recursive: true }, (err) => {
    if (err) {
        console.error('Error creating directories:', err);
        return;
    }
    console.log('Directories created successfully.');
});

सिंक्रोनस तरीके से नेस्टेड डायरेक्टरीज़ क्रिएट करने के लिए:

try {
    fs.mkdirSync('parent-directory/child-directory', { recursive: true });
    console.log('Directories created successfully.');
} catch (err) {
    console.error('Error creating directories:', err);
}

Node.js का फाइल सिस्टम मॉड्यूल डायरेक्टरीज़ के साथ काम करने के लिए आवश्यक सभी सुविधाएं प्रदान करता है। इन ऑपरेशन्स को समझकर, आप अपने एप्लिकेशन की डायरेक्टरी संरचना को प्रभावी ढंग से प्रबंधित कर सकते हैं। अगले सेक्शन में, हम HTTP मॉड्यूल के बारे में जानेंगे और देखेंगे कि कैसे एक बेसिक HTTP सर्वर बनाया जाता है।

एसिंक्रोनस और सिंक्रोनस फाइल ऑपरेशन्स (Asynchronous vs Synchronous File Operations)

Node.js का फाइल सिस्टम मॉड्यूल (File System Module) फाइलों के साथ काम करने के लिए दोनों प्रकार के ऑपरेशन्स प्रदान करता है: एसिंक्रोनस (Asynchronous) और सिंक्रोनस (Synchronous)। यह समझना महत्वपूर्ण है कि ये दो प्रकार के ऑपरेशन्स कैसे काम करते हैं और उनके बीच क्या अंतर है। इस सेक्शन में, हम इन दोनों प्रकार के ऑपरेशन्स को विस्तार से समझेंगे और जानेंगे कि कब किसे उपयोग करना चाहिए।

एसिंक्रोनस फाइल ऑपरेशन्स (Asynchronous File Operations)

एसिंक्रोनस फाइल ऑपरेशन्स नॉन-ब्लॉकिंग होते हैं, जिसका अर्थ है कि वे तुरंत वापस लौटते हैं और फाइल ऑपरेशन को बैकग्राउंड में जारी रखते हैं। जब ऑपरेशन पूरा हो जाता है, तो एक कॉलबैक फंक्शन निष्पादित होता है। एसिंक्रोनस ऑपरेशन्स का उपयोग करते समय, आपका प्रोग्राम अन्य कार्यों को जारी रख सकता है और रुकता नहीं है।

उदाहरण: एसिंक्रोनस तरीके से फाइल पढ़ना (Reading a File Asynchronously)

const fs = require('fs');

fs.readFile('example.txt', 'utf8', (err, data) => {
    if (err) {
        console.error('Error reading the file:', err);
        return;
    }
    console.log('File content:', data);
});

उदाहरण: एसिंक्रोनस तरीके से फाइल लिखना (Writing to a File Asynchronously)

const content = 'This is an example text.';

fs.writeFile('example.txt', content, 'utf8', (err) => {
    if (err) {
        console.error('Error writing to the file:', err);
        return;
    }
    console.log('File has been written successfully.');
});

सिंक्रोनस फाइल ऑपरेशन्स (Synchronous File Operations)

सिंक्रोनस फाइल ऑपरेशन्स ब्लॉकिंग होते हैं, जिसका अर्थ है कि वे तब तक वापस नहीं लौटते जब तक कि ऑपरेशन पूरा नहीं हो जाता। सिंक्रोनस ऑपरेशन्स का उपयोग करते समय, आपका प्रोग्राम उस ऑपरेशन के पूरा होने का इंतजार करता है और इस दौरान कोई अन्य कार्य नहीं करता। ये ऑपरेशन्स सरल होते हैं लेकिन वे आपके एप्लिकेशन की प्रदर्शन को प्रभावित कर सकते हैं यदि वे लंबे समय तक चलने वाले हों।

उदाहरण: सिंक्रोनस तरीके से फाइल पढ़ना (Reading a File Synchronously)

const fs = require('fs');

try {
    const data = fs.readFileSync('example.txt', 'utf8');
    console.log('File content:', data);
} catch (err) {
    console.error('Error reading the file:', err);
}

उदाहरण: सिंक्रोनस तरीके से फाइल लिखना (Writing to a File Synchronously)

const content = 'This is an example text.';

try {
    fs.writeFileSync('example.txt', content, 'utf8');
    console.log('File has been written successfully.');
} catch (err) {
    console.error('Error writing to the file:', err);
}

एसिंक्रोनस और सिंक्रोनस ऑपरेशन्स के बीच अंतर (Differences Between Asynchronous and Synchronous Operations)

  • प्रदर्शन (Performance): एसिंक्रोनस ऑपरेशन्स बेहतर प्रदर्शन प्रदान करते हैं क्योंकि वे नॉन-ब्लॉकिंग होते हैं और आपके प्रोग्राम को अन्य कार्यों को जारी रखने की अनुमति देते हैं। सिंक्रोनस ऑपरेशन्स ब्लॉकिंग होते हैं और आपके प्रोग्राम को रोकते हैं जब तक कि ऑपरेशन पूरा नहीं हो जाता।
  • सरलता (Simplicity): सिंक्रोनस ऑपरेशन्स सिंटैक्स और उपयोग में सरल होते हैं क्योंकि वे सीक्वेंशियल होते हैं और कॉलबैक या प्रॉमिसेस की आवश्यकता नहीं होती।
  • उपयोग के मामले (Use Cases): एसिंक्रोनस ऑपरेशन्स का उपयोग तब करना चाहिए जब आप लंबी चलने वाली फाइल ऑपरेशन्स करते हैं और चाहते हैं कि आपका प्रोग्राम अन्य कार्यों को जारी रखे। सिंक्रोनस ऑपरेशन्स का उपयोग छोटे और त्वरित फाइल ऑपरेशन्स के लिए किया जा सकता है जहां प्रदर्शन महत्वपूर्ण नहीं है।

एसिंक्रोनस और सिंक्रोनस फाइल ऑपरेशन्स दोनों के अपने फायदे और नुकसान हैं। एसिंक्रोनस ऑपरेशन्स उच्च प्रदर्शन और स्केलेबिलिटी प्रदान करते हैं, जबकि सिंक्रोनस ऑपरेशन्स सिंटैक्स में सरलता और उपयोग में आसान होते हैं। यह महत्वपूर्ण है कि आप अपने एप्लिकेशन की आवश्यकताओं के आधार पर सही प्रकार के ऑपरेशन्स का चयन करें। अगले सेक्शन में, हम HTTP मॉड्यूल का परिचय देंगे और देखेंगे कि कैसे एक बेसिक HTTP सर्वर बनाया जाता है।



Table of Contents

Index