আপনি ক্লায়েন্ট সাইডে (ব্রাউজার) জেএস-এ ফাইল পড়তে বা লিখতে পারবেন না। এটি Node.js-এ fs মডিউল ব্যবহার করে সার্ভারসাইডে করা যেতে পারে। এটি ফাইল সিস্টেমে ফাইলগুলি পড়তে এবং লিখতে সিঙ্ক এবং অ্যাসিঙ্ক ফাংশন সরবরাহ করে। আসুন node.js
-এ fs মডিউল ব্যবহার করে ফাইল পড়া এবং লেখার উদাহরণ দেখি।নিচের কোড −
সহ main.js নামে একটি js ফাইল তৈরি করিvar fs = require("fs");
console.log("Going to write into existing file");
// Open a new file with name input.txt and write Simply Easy Learning! to it.
fs.writeFile('input.txt', 'Simply Easy Learning!', function(err) {
if (err) {
return console.error(err);
}
console.log("Data written successfully!");
console.log("Let's read newly written data");
// Read the newly written file and print all of its content on the console
fs.readFile('input.txt', function (err, data) {
if (err) {
return console.error(err);
}
console.log("Asynchronous read: " + data.toString());
});
}); এখন ফলাফল দেখতে main.js চালান -
$ node main.js
আউটপুট
Going to write into existing file Data written successfully! Let's read newly written data Asynchronous read: Simply Easy Learning!
নোডে কীভাবে fs মডিউল কাজ করে এবং কীভাবে এটি ব্যবহার করতে হয় সে সম্পর্কে আপনি https://www.tutorialspoint.com/nodejs/nodejs_file_system.htm এ আরও পড়তে পারেন।