In this article, I’ll be demonstrating you the way, you can use File System to deal with files related operations in Node.js. There are various instances when we need to deal with the files & perform basic operations such as Creating, Reading, Updating, Deleting & Renaming. Well, we’ll be covering all of these basic operations in this article. Let’s first understand the nature of File System module -
File System is a built-in Node.js module & can be imported into the project like this —
var fs = require(‘fs’);
All the methods in this module have synchronous as well as asynchronous forms. Asynchronous methods take first parameter of the callback function as an error and the last parameter as the completion callback. Let’s see through an example -
In the above shown example, we can see that the callback function has two parameters i.e err & data. The first parameter is used to catch any error that occurs & the second parameter returns the response/data after successful execution of the function. Now that we’ve understood the basic usage & nature of File System module, let’s move on to implement some basic operations.
- Creating a new file.
In the above shown example, we’re using fs.open() method to create a new file. The first argument it takes is name of the file. In second argument, a flag has to be defined. If the flag is w for writing, the specified file is opened for writing. Other flags that could be used here include -
- r : Opens the file for reading.
- r+ : Opens the file for reading and writing.
- rs : Opens the file for reading in synchronous mode.
- w : Opens the file for writing.
- a : Opens the file for appending.
- a+ : Opens the file for reading & appending.
Okay, now let’s edit our newly created file & add a few lines into it before proceeding to check next operations.
2. Updating a file.
Now, let’s check how can we update an existing file & append data to it.
In the above shown example, we’re using .append() method to append data to the end of an existing file. As the first argument, it takes the target file name & as the second argument, it takes data to be appended in the end. Let’s see the outcome for the above shown example :
There’s another way for updating the content of a file using .writeFile() method. So, how is .append() different from .writeFile() ? Well, in case of .append(), we add content at end of the existing data. However, in case of .writeFile(), the existing content gets replaced by new specified content. Let’s try that out.
In .writeFile() method, the way of passing the arguments is similar to that of .append() method. Let’s check its output :
In the above shown output, you can see that the .writeFile() method has completely replaced the existing file content with the one specified in its second argument.
3. Reading a file
To read a file using fs module, we can use .readFile() method. It takes name of the target file as its first argument, an encoding format as its second. Its callback function returns error & data read from the file. Let’s check it out -
Let’s check the output for above demonstrated example -
Looks good? Right? So, this way, we can read the content of a file using .readFile() method.
4. Renaming a file
In the above shown example, it is shown that how can we rename a file using .rename() method. The first argument it takes is name of the target file & as second argument, it takes the new name. This function successfully changes the filename from testFile.txt to newTestFile.txt.
5. Deleting a file
In this example, we can see how a file can be deleted using .unlink() method. After the execution of this function, the specified file i.e dataFile.txt is successfully deleted.
So, that’s all about the file system module & its basic operations. I hope I’ve made things simpler for you to understand. To read more simplified articles like this, refer here. Kindly give your feedback that’d help me to bring better & simplified content for you. Thanks for reading!