टेक्स्ट फ़ाइल पढ़ें
प्रोग्राम के बगल में नमूना फ़ाइल को सहेजें। पूरी फाइल को पढ़ें और उसमें शामिल पंक्ति को प्रिंट करें।
fs.readFileSync
एक आसान उदाहरण
फ़ाइल में सहेजे गए शब्द को पढ़ें।
शुरुआत मेंinput.txt
नतीजाhello
hello
declare function require(name: "node:fs"): { readFileSync(path: string, encoding: "utf8"): string };
const text = require("node:fs").readFileSync("input.txt", "utf8");
console.log(text.trimEnd());
hello
विज्ञापन
रोज़मर्रा के उदाहरण
उस पिकअप कोड को पढ़ें जिसे लाइब्रेरी ने अगली पाली के लिए सहेजा था।
शुरुआत मेंinput.txt
नतीजाB12
B12
declare function require(name: "node:fs"): { readFileSync(path: string, encoding: "utf8"): string };
const text = require("node:fs").readFileSync("input.txt", "utf8");
console.log(text.trimEnd());
B12
उस कमरे का नंबर पढ़ें जिसे शिक्षक ने सुबह की कक्षा के लिए सहेजा था।
शुरुआत मेंinput.txt
नतीजाRoom 8
Room 8
declare function require(name: "node:fs"): { readFileSync(path: string, encoding: "utf8"): string };
const text = require("node:fs").readFileSync("input.txt", "utf8");
console.log(text.trimEnd());
Room 8
बस प्रदर्शन फ़ाइल में संग्रहीत अगला पड़ाव दिखाएँ।
शुरुआत मेंinput.txt
नतीजाOak Street
Oak Street
declare function require(name: "node:fs"): { readFileSync(path: string, encoding: "utf8"): string };
const text = require("node:fs").readFileSync("input.txt", "utf8");
console.log(text.trimEnd());
Oak Street