i2tutorials

Javascript File Handling

Javascript File Handling

 

As system file handling functionality is not useful with the common and normal browser. Therefore, node JS has combatted the inaccessibility of file handling in javaScript providing common(same) functionality. Node.js file system has supported the file handling and modifies in its file system which provides inside the file system module of the node.js it maintains some structural hierarchy which useful in this. To use this feature or the module of node.js for maintaining the file system needs or requires the method to import that feature and the functionality.

 

Syntax:

 

var fs = require (‘fs’);

 

Common functions used for handling the files using

⦁ Reading of files
⦁ Creation of files
⦁ Updating files
⦁ Deletion of files
⦁ Renaming files

 

1. Reading of Files

Reading of files is functionality that involves the reading of files 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 the main manipulation activity that needed to be performed under the fs.readFile() function.

 

EXAMPLE:

 

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);A

 

OUTPUT:

 

Javascript File Handling

 

2. Creation of Files

The file system module has some inbuild functions which involve some method for creating new files once pointed will help in the creation of new files. It includes the following js methods for creation which are as follows:

 

EXAMPLE:

 

var fs = require('fs');
fs.writeFile('new_file.txt', 'Hello content!', function (err) {
if (err) throw err;
console.log('Saved!');
});

 

OUTPUT:

 

 

3. fs.open()

fs.open() js method takes a flag as a second argument. If the flag is “w” then it will specifically be provided for writing, in the specified and destined location. otherwise, it is empty, then an empty file is created.

 

4. fs.appendFile()

This js function is providing to append or some relevant and specific contents to a file. If there is no file that exists pre handedly then a new file will get created.

 

5. fs.writeFile()

It is useful to replace the specified file which means it basically overrides the files and then opens a new file. Containing the specified content(text) which is being written in the next section that will be used for overriding something. If no file exists, then it will create a file(document) containing that specific content.

 

6. Updating Files

The file system module makes providing of two methods which include the following methods:

fs.appendFile is used to append the specific file or document at the end of the specified file.
Fs.writeFile method is provided to write the specific content or text to the designated file.

 

EXAMPLE:

 

var fs = require('fs');
fs.appendFile('Update_file.txt', ' This is my text.', function (err) {
if (err) throw err;
console.log('Updated!');
});

 

OUTPUT:

 

 

7. Deletion of File

You want to delete any unwanted file within the file system, the unlink method is used which is fs.unlink method used to delete the method from a specified location of another file.

 

EXAMPLE:

 

var fs = require('fs');
fs.unlink('mynewfile2.txt', function (err) {
if (err) throw err;
console.log('File deleted!');
});

 

OUTPUT:

 

 

8. Renaming a File

fs.rename() method is provided to rename any file within the file system. Then it is used to rename functions within your file system module.

 

EXAMPLE:

 

var fs = require('fs');
fs.rename('mynewfile1.txt', 'myrenamedfile.txt', function (err) {
if (err) throw err;
console.log('File Renamed!');
});

 

OUTPUT:

 

 

Exit mobile version