File System Module: Read Files (readFile()), Create Files (appendFile(), open(), writeFile()), Update Files (appendFile(), writeFile()), Delete Files (unlink()), Rename Files (rename())

fs module server को एक memory देता है: readFile एक err-first callback के साथ, appendFile add करता है (और create भी), writeFile replace करता है, unlink delete करता है, rename rename करता है: registrations आख़िरकार एक restart survive करती हैं।

12 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Amnesia वाला Server

FestConnect का server registrations खूबसूरती से लेता है: और पहले restart पर हर एक को भूल जाता है। वे एक variable में रहती थीं, और variables process के साथ मर जाते हैं।

Mehta-shop economics यहाँ भी apply होती है: data जो matter करता है वह disk पर जाता है। Node का built-in fs (File System) module वह pen है: आपका server हर registration को एक file में लिखेगा, list वापस पढ़ेगा, और दिन के आख़िर में इसे archive करेगा। पाँच operations, एक module, और subject का आख़िरी lesson।

At a glance

fs Verbs (Add-vs-Replace Row दो बार सीखिए)

कामMethodBehaviour
Readfs.readFile(path, 'utf8', cb)Content आपके callback को deliver करता है
CreateappendFile / open(path, 'w') / writeFileतीनों file missing होने पर इसे create करते हैं
Update: Addfs.appendFile(path, text, cb)आख़िर में text ADD करता है
Update: Replacefs.writeFile(path, text, cb)पूरा content REPLACE करता है
Deletefs.unlink(path, cb)File remove करता है
Rename / Movefs.rename(oldPath, newPath, cb)Rename करता है; path में एक नया folder इसे move करता है

Practical

Registration Book, पूरा Cycle

const fs = require('fs');

// CREATE + UPDATE(add): appendFile creates the file if missing
fs.appendFile('registrations.txt', 'Riya: Garba Night\n', function (err) {
    if (err) throw err;              // err-first: check before trusting
    console.log('Registration saved');
});

// READ: asynchronous, content arrives in the callback
fs.readFile('registrations.txt', 'utf8', function (err, data) {
    if (err) throw err;
    console.log('So far:\n' + data);
});

// UPDATE(replace): writeFile wipes and rewrites
// fs.writeFile('winner.txt', 'Team Pixel', function (err) { ... });

// DELETE and RENAME:
// fs.unlink('old-draft.txt', function (err) { ... });
// fs.rename('registrations.txt', 'day1-archive.txt', function (err) { ... });

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Theory

err-First Callback: Node का Handshake

हर fs callback को err पहले argument की तरह मिलता है: सब ठीक होने पर null, ना होने पर एक Error object (file missing, disk full, permission नहीं)।

function (err, data) { if (err) throw err; ... }

यह err-first convention Node-wide law है: data को touch करने से पहले हर बार err check कीजिए: एक unchecked err का मतलब है data undefined है और अगली line crash site बन जाती है। यह BCA403 के exception discipline का Unit 3 है, callback clothing में फिर से जन्मा हुआ: यहाँ errors values हैं, आपको handed, और एक handed error को ignore करना Node में एकमात्र unforgivable style crime है।

Quiz

registrations.txt में 40 entries हैं। Committee एक और add करने के इरादे से fs.writeFile('registrations.txt', 'Riya: Garba Night', cb) चलाती है। बाद में file में क्या है?

  1. 41 entries: आख़िर में नई वाली
  2. सिर्फ़ Riya की एक entry: writeFile ने पूरा content REPLACE कर दिया
  3. 41 entries: top पर नई वाली
  4. एक error: writeFile existing files को touch करने से मना करता है
Show the answer

सिर्फ़ Riya की एक entry: writeFile ने पूरा content REPLACE कर दिया

writeFile whiteboard eraser है: file में जो भी था वह gone है, exactly नए text से replace: 40 registrations एक ग़लत verb से destroy। ADDING appendFile का काम था, और यह add-vs-replace pair exam का favourite fs question और genuinely dangerous real-world slip दोनों है (एक file पर कोई undo नहीं है)। Option D एक safety interlock imagine करता है जो exist नहीं करता: writeFile happily overwrite करता है। Habit: कोई write verb type करने से पहले पूछिए मैं add कर रहा हूँ या replace, और answer method चुनने दीजिए।

Think first

Print Order Predict कीजिए

Listing में, appendFile readFile से PEHLE call होता है। क्या 'Registration saved' 'So far:' से पहले print होना guaranteed है? पहले याद कीजिए कौन सा model पूरे fs को चलाता है, फिर एक answer commit करके tap कीजिए।

Show the answer

Guaranteed नहीं है। दोनों calls asynchronous हैं: हर एक अपना disk work शुरू करता है और तुरंत return होता है; callbacks तब fire होते हैं जब उनका I/O complete होता है, जिस भी order में disk deliver करे: code का top-to-bottom order callbacks के order के बारे में कुछ promise नहीं करता। (इसका मतलब यह भी है readFile append land होने से पहले चल सकता है: शायद नई entry miss करते हुए।) जो काम एक write पर DEPEND करता है वह write के callback के INSIDE belong करता है: append complete होने के बाद वहाँ readFile nest करके read कीजिए। Async model की तीसरी telling: effects, AJAX, अब fs: एक model, पूरा subject।

Watch out

File-Handling Slips

appendFile की जगह writeFile: वह 40-registrations wipe: add बनाम replace, हमेशा consciously choose कीजिए।

err Check Skip करना: एक missing file फिर एक line बाद undefined data पर crash कराती है, असली cause से बहुत दूर।

Line Order से Sequencing: async callbacks आपकी file की top-to-bottom poetry को ignore करते हैं: dependencies callbacks के अंदर nest होती हैं।

Paths जहाँ से node चला उसके Relative हैं: वही app.js दूसरे folder से start होने पर वहीं पढ़ता और लिखता है।

Theory

FestConnect Live, Complete

पूरा subject एक बार walk कीजिए: data ने खुद को describe करना सीखा (XML), page ने move करना सीखा (jQuery), data slim हुआ (JSON), browser ने बिना reload fetch करना सीखा (AJAX), और server ने exist करना, queries answer करना और अब REMEMBER करना सीखा (Node + fs)। एक coherent app, end to end एक language। BCA503-01 अगले साल इस stack को आगे ले जाता है; async model और err-first handshake आपके साथ हर जगह जाते हैं जहाँ JavaScript जाता है।

Summary

Key takeaways

  • require('fs') File System module खोलता है; हर operation एक err-first callback के साथ asynchronous है।
  • Read: readFile(path, 'utf8', cb(err, data)); data touch करने से पहले err check कीजिए।
  • Create: appendFile, open(path, 'w') और writeFile तीनों missing files create करते हैं।
  • Update: appendFile आख़िर में ADD करता है; writeFile सब कुछ REPLACE करता है: consciously choose कीजिए।
  • unlink से delete, rename(oldPath, newPath) से rename (और move)।
  • Callback order I/O completion follow करता है, code order नहीं: dependent काम callbacks के अंदर nest होता है।
  • Memory hook: append adds करता है, write wipes करता है, err पहले बोलता है।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Node.js

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati