Node.JS File System



Node.js File System:

To perform a file operation like creating, reading or writing a file in Node.js, Node.js File system module is a must to be included first.

● To include the File System module:

Syntax:
var variable_name = require(‘fs’);

Opening a file in Node.js:

To open a file in Node.js, fs.open() method is used.

Syntax:
fs.open(path, flags[, mode], callback)

Path: This parameter represents the path of the file to open.

Flags: This parameter specifies the flag that represents the mode of the file to open. The different values of flags are listed below:

Flags DESCRIPTION

 

Flags DESCRIPTION
r Read only mode. Exception occurs if the file does not exist.
w Write only mode. Either a new file is created, if it does not exist or it overwrites the existing file.
a Write only mode. Either a new file is created, if it does not exist or it continues writing in the existing file.
r+ Read Write mode. Exception occurs if the file does not exist.
w+ Read Write mode. Either a new file is created, if it does not exist or it overwrites the existing file.
a+ Read Write mode.  Either a new file is created, if it does not exist or it continues writing in the existing file.
rs Read only mode. Synchronous mode.
wx Write only mode. Either a new file is created, if it does not exist or it fails if file exists.
ax Write only mode. Either a new file is created, if it does not exist or it fails if file exists.
rs+ Read Write mode. Synchronous mode.
wx+ Read Write mode. Either a new file is created, if it does not exist or it fails if file exists.
ax+ Read Write mode. Either a new file is created, if it does not exist or it fails if file exists.

 

Mode: This parameter specifies the mode that can be either read, write or readwrite. The default value is 0666 which represents the readwrite mode.

Callback: When file open operation completes callback function is called with two arguments (err, fd).

Example:

var fs = require(‘fs’);

fs.open(‘example.txt’, ‘w+’, function (err, file)
{
if (err) throw err;
console.log(‘Successfully Opened!’);
});

OUTPUT:

Successfully Opened!

Reading a file in Node.js:

To read a file in Node.js, fs.readFile() method is used.

Writing a file in Node.js:

To write in a file in Node.js, fs.writeFile() method is used.

Appending a file in Node.js:

To append some contents to a file in Node.js, fs.appendFile() method is used.

Deleting a file in Node.js:

To delete a file in Node.js, fs.unlink() method is used.

Renaming a file in Node.js:

To rename an already existing file in Node.js, fs.rename() method is used.

Tags: ,
Leave a comment

Your email address will not be published. Required fields are marked *

Subscribe now

Receive weekly newsletter with educational materials, new courses, most popular posts, popular books and much more!

https://bridgejunks.com/ https://crownmakesense.com/ https://brithaniabookjudges.com/ https://hughesroyality.com/ https://rhythmholic.com/ https://bandar89.simnasfikpunhas.com/ https://www.100calshop.co.il/products/thailand/ https://myasociados.com/ https://solyser.com/ http://konfidence.cz/ https://muscadinepdx.com/ https://bandar89.parajesandinos.com.ve/ https://goremekoop.com/ https://oncoswisscenter.com/ https://www.turunclifehotel.com/bandar89/ https://www.houseofproducts.biz/ https://taimoormphotography.com/
BIJI18 BIJI18 BIJI18