Assignment 3

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Name: Chetan Prakash Birari Roll No: 08

Div: Batch: A1

Assignment No: 3

Read And Write the file in nodejs.

Write File:

var fs = require('fs');

fs.writeFile('new.txt', 'meghana chavan', function (err) {


if (err) throw err;
console.log('created');
});

Read File:

const fs = require('fs');

fs.readFile('new.txt', 'utf8', (err, data) => {


if (err) {
console.error(err);
return;
}
console.log(data);
});

Append File:

1.

const fs = require('fs');

fs.appendFile('new.txt', 'student at kkw', function (err) {


if (err) throw err;
console.log('Saved!');
});

2.

const fs = require('fs');

console.log("\nFile Contents of file before append:",


fs.readFileSync("new.txt", "utf8"));

fs.appendFile("new.txt", "student of K.K.Wagh College", (err) => {


if (err) {
console.log(err);
}
else {

console.log("\nFile Contents of file after append:",


fs.readFileSync("new.txt", "utf8"));
}
});

You might also like