Skip to content

Instantly share code, notes, and snippets.

@ishaadX
Last active August 29, 2015 14:27
Show Gist options
  • Save ishaadX/88e5966b00ab5434c958 to your computer and use it in GitHub Desktop.
Save ishaadX/88e5966b00ab5434c958 to your computer and use it in GitHub Desktop.
Node JS এর ফাইল সিস্টেম ঃ read, write, rename, delete
//রিড ফাইল
var fs = require('fs'); // Node module er file system ta k include korese
fs.readFile('/Users/Shaad/Desktop/Node/node.rtf', 'utf8', function(err, data){ //fs.readFile diye file read option initate korese, then convert it into utf8 format
if (err) {
console.log(err); //if kono error thake tahole console a err msg show korbe
}
console.log(data) // otherwise data read kore file content show korbe
});
// ফাইল রাইট করা
var fs = require('fs');
fs.createReadStream('node.rtf').pipe(fs.createWriteStream('node1.txt'));
// ফাইল রিনেইম করা
var fs = require('fs');
fs.rename ('/Users/Shaad/Desktop/Node/node1.txt', '/Users/Shaad/Desktop/Node/nodejs.txt', function(err) {
if (err) throw err;
console.log('rename complete');
});
//ফাইল ডিলেট করা
var fs = require('fs');
fs.unlink('/Users/Shaad/Desktop/Node/nodejs.txt', function (err) {
if (err) throw err;
console.log('successfully deleted /Users/Shaad/Desktop/Node/nodejs.txt');
});
***********************************************************
Combinedly Done!
***********************************************************
var fs = require('fs');
fs.writeFile('/Users/Shaad/Desktop/Node/node.txt', 'Hello Node', function (err) {
if (err) throw err;
console.log('It\'s saved!');
fs.createReadStream('node.txt').pipe(fs.createWriteStream('delete.txt'));
fs.readFile('/Users/Shaad/Desktop/Node/node.txt', 'utf8', function(err, data){ //fs.readFile diye file read option initate korese, then convert it into utf8 format
if (err) {
console.log(err); //if kono error thake tahole console a err msg show korbe
}
console.log(data) // otherwise data read kore file content show korbe
});
fs.appendFile('/Users/Shaad/Desktop/Node/node.txt', '...whatssapp!', encoding='utf8', function (err) {
if (err) throw err;
console.log("data appended");
});
fs.rename ('/Users/Shaad/Desktop/Node/node.txt', '/Users/Shaad/Desktop/Node/nodejs.txt', function(err) {
if (err) throw err;
console.log('rename complete');
});
fs.unlink('/Users/Shaad/Desktop/Node/delete.txt', function (err) {
if (err) throw err;
console.log('successfully deleted /Users/Shaad/Desktop/Node/delete.txt');
});
});
***************************************************
Output: কিছু কনফিউশন আছে
It's saved!
rename complete
successfully deleted /Users/Shaad/Desktop/Node/delete.txt
data appended
Hello Node
****************************************************
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment