Node.JS File System

Node.js File System module is used to perform file I/O operations in node.js. To import file system module, require command is used as follows: “var filesystem = require(“fs”)”.

All the functions of the “fs” have two variations. There are synchronous as well as their asynchronous counterparts. Asynchronous functions take a couple of parameters: First parameter is the callback in case if error occurs and the second function is the callback function which is called when function completes its execution. All the synchronous functions are blocking where as asynchronous functions are non-blocking. Therefore, you should always prefer asynchronous functions.

Example of Synchronous vs Asynchronous Functions

Take a look at the following example. Here we shall explain what is the difference between a synchronous and asynchronous function.

Create a text file named “myfile.txt” and add the following text in the file.

Knowledge hills is a website that contains free of cost programming tutorials

Learn and Build apps for free.

Now create a file named “filesystem1.js” in the same directory as “myfile.txt” and add the following line of code in it.

var fileSystem = require("fs");

// Asynchronous File Reading
fileSystem.readFile('myfile.txt', function (err, content) {
   if (err) {
      return console.error(err);
   }
   console.log("Asynchronous File Reading: " + content.toString());
});

// Synchronous File Reading
var content = fileSystem.readFileSync('myfile.txt');
console.log("Synchronous File Reading: " + content.toString());

console.log("Program execution completed.")

Download the code Try the code

Take a careful look at the above code. First we import the “fs” module and the call readFile() function on it. This is asynchronous function. Once it start reading the file, program execution shifts to the next piece of code. Here we use readFileSync() function which is a synchronous function. It will also read file. However, it will block the code until all the file is read. It then prints the contents of the file on console. Next when the first asynchronous call readFile() is complete reading the data. The callback function is executed which prints the contents of the file. Therefore, you can see how synchronous and asynchronous methods differ in terms of execution.

Opening a file via Node.js File System

To open a file via node.js file system module, the open function is used. This function takes four parameters: Path to the file, file permissions such as reading, writing or both, the mode and finally the callback with two arguments err and fd.

Take a look at the following example to see how to open a file via node.js file system.

var fileSystem = require("fs");

console.log("Before opening the file");
fileSystem .open('myfile.txt', 'r+', function(err, fd) {
   if (err) {
      return console.error(err);
   }
  console.log("After successfully opening the file.");     
});

Download the code Try the code

In the above code the flag used for opening the file is r+. This means that file should be asynchronously opened for both reading and writing purposes.

<<< Node.JS StreamsNode.JS File System Useful Functions >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .