Node.JS File System Useful Functions

In the previous article we saw what node.js file system module is. We also saw how to use asynchronous as well synchronous functions of node.js file system module with the help of an example. Finally we saw how fs “open” function is used to open any file. In this article we shall some more node.js file system useful functions.

Node.js File System Reading-Writing

To read contents of the file, the best way is to use “readFile()” which is asynchronous and takes two parameters. Error event and callback. Similarly to write data to a file, use “writeFile” function which is also asynchronous. Take a look at the following example to see how file reading/writing tasks are accomplished by File System module.

var fileSystem = require("fs");

console.log("Data Writing..");
fileSystem.writeFile('output3.txt', 'Knowledge Hills, Learning Platform',  function(err) {
   if (err) {
      return console.error(err);
   }
   
   console.log("Data writing complete");
   console.log("Data reading");
   fileSystem.readFile('output3.txt', function (err, data) {
      if (err) {
         return console.error(err);
      }
      console.log("Reading data asynchronously: " + data.toString());
   });
});

Download the code Try the code

From the above code, it can be seen that writeFile method takes three parameters: the path of the file, the data to be written on the file and error function as call back. Now if you go into the directory where you have placed your code file, you should see a file named “output3.txt” and the file should contain the string you passed as second parameter to the writeFile() function.

Creating and Removing Directory

Since we are discussing, node.js file system useful functions, it wouldnt be fair if we dont discuss directory functions. The file system module contains “mkdir” and “rmdir” functions that are respectively used to create and remove directories in node.js. Take a look at the following example. Here we create two directories.

var fileSystem = require("fs");

console.log("Creating new director /newdir/temp");
fileSystem.mkdir('/newdir',function(err){
   if (err) {
      return console.error(err);
   }
} )
fileSystem.mkdir('/newdir2',function(err){
   if (err) {
      return console.error(err);
   }
})

 console.log("New directories created.");

Download the code Try the code

You can see that the “mkdir” function takes two parameters. The path of the directory to create and the error callback function. Now if you go to your “C:” directory, you should find two new directories there i.e. newdir, newdir2.

Take a look at the following code, this will remove “newdir” in the “C:” drive. If directory doesnt exist, an exception shall be thrown.

var fileSystem = require("fs");

console.log("deleting director /newdir");
fileSystem.rmdir('/newdir',function(err){
   if (err) {
      return console.error(err);
   }
})
    
console.log("Directory removed successfully ...");

Download the code Try the code

Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .