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 :

  1. 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);
});
  1. 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

  1. 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!');
    });
    
  2. 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

  1. 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');
    });
    
  2. 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:

  1. 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');
    
  2. 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

  1. 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');
    });
    
  2. 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
    1. fs.readFile() : Read file in asynchronous way.
    2. fs.readFileSync() : Read file in synchronous way.
  • Write file operation using nodejs
    1. fs.writeFile() : Write file in asynchronous way.
    2. fs.writeFileSync() : Write file in synchronous way.
  • Append file operation using nodejs
    1. fs.appendFile() : Append file in asynchronous way.
    2. fs.appendFileSync() : Append file in synchronous way.
  • Rename file operation using nodejs
    1. fs.rename() : Rename file name in asynchronous way.
    2. fs.renameSync() : Rename file name in synchronous way.
  • Delete (unlink) file operation using nodejs
    1. fs.unlink() : Delete file in asynchronous way.
    2. fs.unlinkSync() : Delete file in synchronous way.

猜你喜欢

转载自www.cnblogs.com/PrimerPlus/p/12970042.html