Append data to a File using Nodejs



The fs.appendFile() method is used to asynchronously append the given data to a file. A new file is created if it does not exist.


Syntax:

fs.appendFile( path, data[, options], callback )

Code


// Import the filesystem module
const fs = require("fs");
// Get the file contents before the append operation
  fs.writeFile("read.txt","today is monday", (err) => {

      console.log(" file is created")
      console.log(err);
  });

  fs.appendFile('read.txt',"This is used for append text in to read.txt file", (err) => {
                 console.log("text appended");
  });
// Get the file contents after the append operation 
 fs.readFile("read.txt","UTF-8",(err, data) =>{
                console.log(data);
    });

Run Code

PS C:\node js\fsAsync> node index.js

Output

today is monday:this is used for append text in to read.txt file

Contact us for this nodejs assignment Solutions by Codersarts Specialist who can help you mentor and guide for such nodejs assignments.


If you have project or assignment files, You can send at contact@codersarts.com directly

Contact Us

Tel: (+91) 0120  4118730  

Time :   10 : 00  AM -  08 : 00 PM IST 

Registered address: G-69, Sector 63, 

 Noida - 201301, India

We Provide Services Across The different countries

USA    Australia   Canada   UK    UAE    Singapore   New Zealand    Malasia   India   Ireland   Germany

Terms and Conditions        Privacy Policy    Pricing Policy       Refund Policy         COPYRIGHT © 2021

Sofstack Technology Solutions Pvt. Ltd.

  • CodersArts | Linkedin
  • Instagram