Append data to a File using Node FS - Tutorial Kart
Append data to a File using Node FS
Node.js Append to a File
To append data to file in Node.js, use Node FS appendFile() function for asynchronous file operation or Node
FS appendFileSync() function for synchronous file operation.
In this Node.js Tutorial, we shall learn
Syntax of appendFile() function
Syntax of appendFileSync() function
Example for appendFile() : Appending data to file Asynchronously
Example for appendFileSync() : Appending data to file Synchronously
Syntax of appendFile()
fs.appendFile(filepath, data, options, callback_function);
Callback function is mandatory and is called when appending data to file is completed.
Syntax of appendFileSync()
fs.appendFileSync(filepath, data, options);
where :
filepath [mandatory] is a String that specifies file path
data [mandatory] is what you append to the file
options [optional] to specify encoding/mode/flag
Note : If file specified does not exist, a new file is created with the name provided, and data is appended to the
file.
Example : Node.js Append data to file asynchronously using appendFile()
To append data to a file asynchronously in Node.js, use appendFile() function of Node FS as shown below :
nodejs-append-to-file-example.js
// Example Node.js
program to append
data to file
var fs = require('fs');
// Example Node.js program to append data to file
var data = "\nLearn
var fs =with
require('fs');
Node.js
the help
of well built Node.js
Tutorial.";
var data = "\nLearn Node.js with the help of well built Node.js Tutorial.";
// append data to file
fs.appendFile('sample.t
xt',data,
// append'utf8',
data to file
// callback function
fs.appendFile('sample.txt',data,
'utf8',
function(err) {
if //
(err)
throw function
err;
callback
// if no error
function(err) { is
console.log("Data
appended
to file
if (err)
throw err;
successfully.")
}); // if no error
console.log("Data is appended to file successfully.")
});
Terminal Output
arjun@arjunVPCEH26EN:~/nodejs$
node nodejs-appendto-file-example.js
Data
is appended to
arjun@arjun-VPCEH26EN:~/nodejs$
node nodejs-append-to-file-example.js
file successfully.
Data is appended to file successfully.
File before appending
Welcome to
.
Welcome to .
File after appending
Welcome to
.
Learn Node.js with the
help of well built
Node.js
WelcomeTutorial.
to .
Learn Node.js with the help of well built Node.js Tutorial.
Example : Node.js Append data to file synchronously using appendFileSyc()
To append data to a file synchronously in Node.js, use appendFileSync() function of Node FS as shown below
:
nodejs-append-to-file-example-2.js
// Example Node.js
program to append
data to file
var fs = require('fs');
// Example Node.js program to append data to file
var data = "\nLearn
var fs =with
require('fs');
Node.js
the help
of well built Node.js
Tutorial.";
var data = "\nLearn Node.js with the help of well built Node.js Tutorial.";
// append data to file
fs.appendFileSync('sam
ple.txt',data,
'utf8');
// append data
to file
console.log("Data is
fs.appendFileSync('sample.txt',data,
'utf8');
appended
to file
successfully.")
console.log("Data is appended to file successfully.")
Terminal Output
arjun@arjunVPCEH26EN:~/nodejs$
node nodejs-appendto-file-example-2.js
Data
is appended to
arjun@arjun-VPCEH26EN:~/nodejs$
node nodejs-append-to-file-example-2.js
file successfully.
Data is appended to file successfully.
File before appending
Welcome to
.
Welcome to .
File after appending
Welcome to
.
Learn Node.js with the
help of well built
Node.js
WelcomeTutorial.
to .
Learn Node.js with the help of well built Node.js Tutorial.
Conclusion :
In this tutorial ¨C Node.js Append to a File, we have learnt to append data to a file in Node.js, synchronously
and asynchronously using appendFileSync() and appendFile() functions of Node FS respectively with Example
Node.js programs.
Node.js
?
Node.js Tutorial
Get Started W ith Node.js
Get Started W ith Node.js
?
Install Node.js Ubuntu Linux
?
Install Node.js Windows
?
Node.js - Basic Example
?
Node.js - Command Line Arguments
?
Node.js - Modules
?
Node.js - Create a module
?
Node.js - Add new functions to Module
?
Node.js - Override functions of Module
?
Node.js - Callback Function
?
Node.js - forEach
Express.js
?
Express.js Tutorial
?
What is Express.js?
?
Express.js Application Example
?
Install Express.js
?
Express.js Routes
?
Express.js Middleware
?
Express.js Router
Node.js Buffers
?
Node.js Buffer - Create, Write, Read
?
Node.js Buffer - Length
?
Node.js - Convert JSON to Buffer
?
Node.js - Array to Buffer
Node.js HTTP
?
Node.js - Create HTTP Web Server
?
Node.js - Redirect URL
Node.js MySQL
?
Node.js MySQL
?
Node.js MySQL - Connect to MySQL Database
?
Node.js MySQL - SELECT FROM
?
Node.js MySQL - SELECT WHERE
?
Node.js MySQL - ORDER BY
?
Node.js MySQL - INSERT INTO
?
Node.js MySQL - UPDATE
?
Node.js MySQL - DELETE
?
Node.js MySQL - Result Object
Node.js MongoDB
?
Node.js MongoDB
?
Node.js - Connect to MongoDB
?
Node.js - Create Database in MongoDB
?
Node.js - Drop Database in MongoDB
?
Node.js - Create Collection in MongoDB
?
Node.js - Delete Collection in MongoDB
?
Node.js - Insert Documents to MongoDB Collection
?
MongoError: failed to connect to server
Node.js Mongoose
?
Node.js Mongoose Tutorial
?
Node.js Mongoose - Installation
?
Node.js Mongoose - Connect to MongoDB
?
Node.js Mongoose - Define a Model
?
Node.js Mongoose - Insert Single Document to MongoDB
?
Node.js Mongoose - Insert Multiple Documents to MongoDB
Node.js URL
?
Node.js - Parse URL parameters
Node.js FS (File System)
?
Node FS
?
Node FS - Read a File
?
Node FS - Create a File
?
Node FS - Write to a File
?
Node FS - Append to a File
?
Node FS - Rename a File
?
Node FS - Delete a File
?
Node FS Extra - Copy a Folder
Node.js JSON
?
Node.js Parse JSON
?
Node.js Write JSON Object to File
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- append data to csv python
- matlab append data to array
- python append data to dataframe
- how to append data in access
- write output to a file python
- writing to a file python
- append to text file python
- append line to file python
- append data to file python
- append a file in python
- append items to a list
- python append data to a list