Updated March 31, 2023
Introduction to File Handling in JavaScript
File handling in JavaScript is a technique where file being written in html format using CSS can be either called using external html file or using .js file pointed to that html file. Manipulation of file handling in JavaScript involves opening of file, closing of file, Updating data in file. JavaScript file handling does not get compiled by any compiler. It just needs an environment with a working browser where the translator can handle the java script code for the web browser. But all the file handling functions require node Js mandatorily to perform the basic functions which is not possible using normal web browser and html files.
Functions of File Handling in JavaScript
As file handling functionality is not possible with the common and normal browser therefore node JS has combatted that inaccessibility of file handling in java Script using common functionality. Node.js file system has helped and provisioned the file handling manipulations in its file system which means inside the file system module of the node.js it maintains some structural hierarchy which helps in this. To use this feature or the module of node.js for maintaining the file system needs or requires method to import that feature and the functionality.
Let’s go through the functionality of each of the manipulations of the file system module of the Node.js which helps in the file handling functions of JavaScript. Before that look how to include any of the file inside the node.js file system module to perform the manipulation. require() method is used to include the files using the node.js feature for file handling.
Syntax:
var fs = require('fs');
Here the variable is the fs which will be indicated through the node.js that it is pointing the fact it needs file for performing the manipulations and different functions on it.
Common functions used for handling the files using java script includes many functions which are as follows and quite common to be performed on daily basis.
- Reading of files
- Creation of files
- Updating files
- Deletion of files
- Renaming files
1. Reading of Files
Reading of files is a functionality which involves reading of file which can be performed using fs.readFile(). This method fs.readFile() is used to read files on your computer and it is being kept a constraint that your HTML file is being kept in the same folder as Node.js. Creation of a node.js file and reading the HTML file and then returning the content would be main manipulation activity needed to be performed under the fs.readFile() function.
2. Creation of Files
The file system module has some inbuild functions which involves some method for creating new files once pointed will help in creation of new files. It includes the following methods for creation which are as follows:
3. fs.open()
fs.open() method takes a flag as a second argument, if the flag is “w” then it will specifically be used for writing, in the specified and destined location. If it is empty, then an empty file is created.
4. fs.appendFile()
This function is used to append or some relevant and specific contents to a file. If there is no file which exists pre handedly then a new file will get created.
5. fs.writeFile()
It is used to replace the specified file that means it basically overrides the files and then opens a new file containing the specified content which is being written in next section that will be used for overriding something. If no file exists, then it will create a file containing that specific content.
6. Updating Files
The file system module makes use of two methods which includes following methods:
- fs.appendFile is used to append the specific file at the end of the specified file.
- Fs.writeFile method is used to write the specific content to the designated file.
7. Deletion of File
To delete any unwanted file within the file system, unlink method is used which is fs.unlink method used to delete the method from a specified location of another file.
8. Renaming a File
fs.rename() method is used to rename any file within the file system and then it is used to rename functions within the file system module.
Examples of File Handling in JavaScript
Below are the examples of File Handling in JavaScript:
Example #1
This function in the given program is used to is used for Reading of a file.
Code:
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
fs.readFile('read_file.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end();
});
}).listen(8080);
Output:
Example #2
This function in the given program is used for creating a file within the file system module of the node.js.
Code:
var fs = require('fs');
fs.writeFile('new_file.txt', 'Hello content!', function (err) {
if (err) throw err;
console.log('Saved!');
});
Output:
Example #3
This function in the given program is used for updating and overriding a file within a file system module.
Code:
var fs = require('fs');
fs.appendFile('Update_file.txt', ' This is my text.', function (err) {
if (err) throw err;
console.log('Updated!');
});
Output:
Example #4
This function in the given program is used to delete the file from the file system module.
Code:
var fs = require('fs');
fs.unlink('mynewfile2.txt', function (err) {
if (err) throw err;
console.log('File deleted!');
});
Output:
Example #5
This function in the given program is used to rename a file present within the file system module of the node.js.
Code:
var fs = require('fs');
fs.rename('mynewfile1.txt', 'myrenamedfile.txt', function (err) {
if (err) throw err;
console.log('File Renamed!');
});
Output:
Note: To perform all these file handling manipulations using JavaScript can be performed using Node.js environment therefore, it is mandatory to install the Node.js in a local environment to get proper functionality and not to go much complex functionality of using windows with a local file system. Any activity related to file handling will become cumbersome.
Conclusion
File handling in any language is a very important function as it directly deals with resource management and hierarchical structure within the systems. Therefore, it is very much needed to maintain it using a proper reusable file system module which is present within the node.js file system.
Recommended Articles
This is a guide to File Handling in JavaScript. Here we discuss the Functions of file Handling and its Examples along with its Code Implementation in Javascript. You can also go through our other suggested articles to learn more –