Loading, please wait...

Node.js File System

Node.js includes fs module is used to access a physical file system. This fs module is responsible for all the asynchronous or synchronous file I/O system. Every method used in this file system has synchronous as well as asynchronous forms.

Create a text file named input2.txt with this content:

Tutorials Link

Let us create the node.js file

Reading File

Asynchronous reading:

Run the above code:

 Synchronous reading

In input2.txt file.

Run the node.js

Writing File in Node.js:                        

Use fs.writeFile() method to write data to a file. If file already exists of this name then it overwrites the existing content and if file name is new then it creates a new file and writes data into it.

Syntax:  fs.writeFile( filename ,data[ , options] , callback)

Description of various parameters:

  • Path: This parameter define path and name of the file.
  • Data: This is the String or Buffer to be written into the file
  • Options: This parameter can be an object or string, which can be encoding, mode and flag.default encoding is utf8 and default flag is " r".
  • Callback – This parameter used a function with single parameters err that returns an error in case of any writing error.

Example –Let us create a js file named node.js having the following code :

Run the above code:

Explanation:

fs.writeFile('app.txt' , ' Tutorials Link is E-learning portal!!!!',  function(err) {

   if (err) {

      return console.error(err);

   }

 

When you will run the code (node.js), Tutorials Link is E-learning portal!!!! , this line will be automatically written in app.txt file.

Reading a File

Following is the syntax  to read from a file:

fs.read( fd, buffer, offset ,length , position, callback)

This method will use file descriptor to read the file. If you want to read the file directly using the file name, then you can use another method variable.

Parameters:

Here is the description of the parameters used −

  • fd− This is a parameter used in the file descriptor returned by fs.open().
  • Buffer− This is a parameter used in the buffer that the data will be written to.
  • Offset− This is a parameter used in the offset in the buffer to start writing it.
  • Length− This is a parameter used in an integer specifying the number of bytes to read.
  • Position− This is a parameter used in an integer specifying where to begin reading from in the file. If the position is null, data will be read from the current file position.
  • Callback− This is a parameter used in the callback function which gets the three arguments, (err, bytesRead, buffer).

Example:

In app.text

Run the node.js file

Truncate a File

Syntax: fs.ftruncate(fd,len,callback)

Parameters description

  • fd− This is the file descriptor parameter returned by fs.open().
  • len− This parameter defined the length of the file after which the file will be truncated.
  • Callback− This is a parameter used in the callback function No arguments other than a possible exception are given to the completion callback.

Example:

In app.txt file-

 After run the node.js file, it truncates the app.txt file.

Delete a File Following is the syntax of the method to delete a file −

fs.unlink(path, callback)

Parameters description

  • Path− This is a parameter used in the file name including the path.
  • Callback− This is the callback function parameter  No arguments other than a possible exception are given to the completion callback.

Example

Let us create a js file named node.js having the following code –

Run above code:

After run node.js file , app.txt will be deleted.

  • Get File Information

Syntax:

Parameters

  • Path – This is the string having file name including the path.
  • Callback – This is the callback function which gets two arguments which are err and stats where stats is an object of fs.Stats type.

Apart from the important attributes, there are several useful methods available in fs.Stats class.which can be used to check file type. These methods are given in the table.

 Example: Create fileInfo.js

Run the code node fileInfo.js

  • Create a Directory Syntax

Parameters

  • Path− This is a parameter used in the directory name including the path.
  • Mode− This is the directory permission to be set. Defaults to 0777.
  • Callback− This is the callback function parameter No arguments other than a possible exception are given to the completion callback.
  •  

Example: Let us create a js file named check.js

Run the check.js file