Day 2 File System
30 Days of Node
Introduction
Node.js gives the functionality of File I/O by providing wrappers around the standard POSIX functions. In Node.js, File I/O methods can be performed in both synchronous as well as asynchronous form depending upon the user requirements. In order to use this functionalities we need to require the fs module as shown below :
const fs = require('fs');
Reading a File in Node.js
There are two ways for Reading a file in node.js :
- Reading the file Asynchronously :
// Reading a File Asynchronously using node.js
const fs = require('fs');
fs.readFile('message.txt', (err, data) => {
if (err)
throw err;
console.log('Content : ' + data);
});
-
Reading the file Synchronously
const fs = require('fs'); // Name of the file to be read const filename = 'content.txt'; const content = fs.readFileSync(filename); console.log('Content: ' + content);
Write a File in Node.js
There are two ways for writing a file in Node.js
-
Writing the file Asynchronously
// Writing a File Asynchronously using node.js const fs = require('fs'); const content = "this is the content in the file"; fs.writeFile('message.txt', content, (err) => { if (err) throw err; console.log('It\'s saved!'); });
-
Writing the file Synchronously
const fs = require('fs'); const content = "We are writing this file synchronously using node.js"; fs.writeFileSync('content.txt', content); console.log('File Written Successfully');
Append a File using Node.js
There are two ways for Appending a file using node.js
-
Appending the file Asynchronously:
const fs = require('fs'); const new_data = "This data will be appended at the end of the file."; fs.appendFile('input.txt', new_data, (err) => { if(err) throw err; console.log('The new_content was appended successfully'); });
-
Appending the file Synchronously
// file append operation is node.js const fs = require('fs'); const content = "We are Appending this file synchronously using node.js" fs.appendFileSync('input.txt', content); console.log('File Appended Successful');
Rename a File in Node.js
There are two ways for Renaming a file in node.js:
-
Renaming the file Asynchronously:
const fs = require('fs'); // you have to pass the Relative path of the file from the current working directory. fs.rename('data.txt', 'new_data.txt', (err) => { if (err) throw err; console.log('File renamed successfully!'); }); // To check it's Asynchronous nature! console.log('This method is Asynchronous');
-
Renaming the file Synchronously:
const fs = require('fs'); // you have to pass the Relative path of the file from the Current working directory fs.renameSync('data.txt', 'newData.txt'); console.log('File renamed successfully!'); // To check it's Synchronous nature console.log('This method is Synchronous');
Delete a File in Node.js
There are two ways for deleting a file in Node.js
-
Deleting the file Asynchronously:
const fs = require('fs'); const filename = 'content.txt'; fs.unlink(filename, (err) => { if (err) throw err; console.log('File deleted successfully'); });
-
Deleting the file Synchronously:
const fs = require('fs'); const filename = 'data.txt'; fs.unlinkSync(filename); console.log('File Deleted Successfully');
Summary
In this part of node.js tutorial series we learned about file system in node.js which includes :
- Introduction to file system
- Read file operation using nodejs
- fs.readFile() : Read file in asynchronous way.
- fs.readFileSync() : Read file in synchronous way.
- Write file operation using nodejs
- fs.writeFile() : Write file in asynchronous way.
- fs.writeFileSync() : Write file in synchronous way.
- Append file operation using nodejs
- fs.appendFile() : Append file in asynchronous way.
- fs.appendFileSync() : Append file in synchronous way.
- Rename file operation using nodejs
- fs.rename() : Rename file name in asynchronous way.
- fs.renameSync() : Rename file name in synchronous way.
- Delete (unlink) file operation using nodejs
- fs.unlink() : Delete file in asynchronous way.
- fs.unlinkSync() : Delete file in synchronous way.