Node.Js File System
Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js helps developers to write JavaScript code to run on the server side, generate dynamic content, and deliver to web clients. The two features that make Node.js stand out are:
- Event-driven
- Non-blocking I/O model
About Node.js file system: To handle file operations like creating, reading, deleting, etc., Node.js provides an inbuilt module called FS (File System). Node.js gives the functionality of file I/O by providing wrappers around the standard POSIX functions. All file system operations can have synchronous and asynchronous forms depending upon user requirements. To use this File System module, use the require() method:
var fs = require('fs');
A common use for the Node.Js File System module:
- Read Files
- Write Files
- Append Files
- Close Files
- Delete Files
What are the Synchronous and Asynchronous approaches?
- Synchronous approach: They’re called blocking functions as it waits for each operation to complete, only after that, it executes the coming operation, hence blocking the coming command from prosecution i.e. a command won’t be executed until & unless the query has finished executing to get all the result from former commands.
- Asynchronous approach: They’re called non-blocking functions as it noway waits for each operation to complete, rather it executes all operations in the first go itself. The result of each operation will be handled once the result is available i.e. each command will be executed soon after the prosecution of the former command. While the former command runs in the background and loads the result once it’s finished recycling the data.
Use cases:
- If your operations aren’t doing veritably heavy lifting like querying huge data from DB also go ahead with the Coetaneous way or else Asynchronous way.
- In an Asynchronous way, you can show some progress index to the stoner while in the background you can continue with your heavyweight workshop. This is an ideal script for GUI-grounded apps.
- Example of asynchronous and synchronous: Create a text file named input.txt with the following content:
GeekyExpert: A computer science portal
- Now let us create a js file named main.js with the following code:
var fs = require("fs");
// Asynchronous read
fs.readFile('input.txt', function (err, data) {
if (err) {
return console.error(err);
}
console.log("Asynchronous read: " + data.toString());
});
Output:
Asynchronous read: GeekyExpert: A computer science portal
var fs = require("fs");
// Synchronous read
var data = fs.readFileSync('input.txt');
console.log("Synchronous read: " + data.toString());
Output:
Synchronous read: GeekyExpert: A computer science portal
Open a File:
The fs.open() method is used to create, read, or write a file. The fs.readFile() method is only for reading the file and fs.writeFile() method is only for writing to the file, whereas fs.open() method does several operations on a file. First, we need to load the fs class which is a module to access the physical file system.
fs.open(path, flags, mode, callback)
Parameters:
- path: It holds the name of the file to read or the entire path if stored at other locations.
- flags: Flags indicate the behavior of the file to be opened. All possible values are ( r, r+, rs, rs+, w, wx, w+, wx+, a, ax, a+, ax+).
- mode: Sets the mode of file i.e. r-read, w-write, r+ -readwrite. It sets to default as read-write.
- err: If any error occurs.
- data: Contents of the file. It is called after the open operation is executed.
Example:
Let us create a js file named main.js having the following code to open a file input.txt for reading and writing.
var fs = require("fs");
// Asynchronous - Opening File
console.log("opening file!");
fs.open('input.txt', 'r+', function(err, fd) {
if (err) {
return console.error(err);
}
console.log("File open successfully");
});
Output:
opening file!
File open successfully
Reading a File:
The fs.read() method is used to read the file specified by FD. This method reads the entire file into the buffer.
Syntax:
fs.read(fd, buffer, offset, length, position, callback)
Parameters:
- FD: This is the file descriptor returned by fs.open() method.
- buffer: This is the buffer that the data will be written to.
- offset: This is the offset in the buffer to start writing at.
- length: This is an integer specifying the number of bytes to read.
- position: This is an integer specifying where to begin reading from in the file. If the position is null, data will be read from the current file position.
- callback: It is a callback function that is called after reading of the file. It takes two parameters:
- err: If any error occurs.
- data: Contents of the file.
Example:
Let us create a js file named main.js having the following code:
var fs = require("fs");
var buf = new Buffer(1024);
console.log("opening an existing file");
fs.open('input.txt', 'r+', function(err, fd) {
if (err) {
return console.error(err);
}
console.log("File opened successfully!");
console.log("reading the file");
fs.read(fd, buf, 0, buf.length, 0, function(err, bytes){
if (err){
console.log(err);
}
console.log(bytes + " bytes read");
// Print only read bytes to avoid junk.
if(bytes > 0){
console.log(buf.slice(0, bytes).toString());
}
});
});
Output:
opening an existing file
File opened successfully!
reading the file
40 bytes read
GeekyExpert: A computer science portal
Writing to a File:
This method will overwrite the file if the file already exists. The fs.writeFile() process is used to asynchronously write the fixed data to a file. By default, the file would be replaced if it exists. The ‘options’ parameter can be used to modify the functionality of the method.
Syntex:
fs.writeFile(path, data, options, callback)
Parameters:
- path: It is a string, Buffer, URL, or file description integer that denotes the path of the file where it has to be written. Utilizing a file descriptor will make it behave similarly to fs.write() method.
- data: It is a string, Buffer, TypedArray, or DataView that will be written to the file.
- options: It is a string or object that can be used to specify optional parameters that will affect the output. It has three optional parameters:
- encoding: It is a string value that specifies the encoding of the file. The default value is ‘utf8’.
- mode: It is an integer value that specifies the file mode. The default value is 0o666.
- flag: It is a string value that specifies the flag used while writing to the file. The default value is ‘w’.
- callback: It is the function that would be called when the method is executed.
- err: It is an error that would be thrown if the operation fails.
Example:
Let us create a js file named main.js having the following code:
var fs = require("fs");
console.log("writing into existing file");
fs.writeFile('input.txt', 'GeekyExpert', function(err) {
if (err) {
return console.error(err);
}
console.log("Data written successfully!");
console.log("Let's read newly written data");
fs.readFile('input.txt', function (err, data) {
if (err) {
return console.error(err);
}
console.log("Asynchronous read: " + data.toString());
});
});
Output:
writing into existing file
Data written successfully!
Let's read newly written data
Asynchronous read: GeekyExpert
Appending to a File:
The fs.appendFile() method is used to synchronously append the data to the file.
fs.appendFile(filepath, data, options, callback);
OR
fs.appendFileSync(filepath, data, options);
Parameters:
- filepath: It is a String that specifies the file path.
- data: It is mandatory and it contains the data that you append to the file.
- options: It is an optional parameter that specifies the encoding/mode/flag.
- Callback: The function is mandatory and is called when appending data to a file is completed.
Example 1:
Let us create a js file named main.js having the following code:
var fs = require('fs');
var data = "\nLearn Node.js";
// Append data to file
fs.appendFile('input.txt', data, 'utf8',
// Callback function
function(err) {
if (err) throw err;
// If no error
console.log("Data is appended to file successfully.")
});
Output:
Data is appended to file successfully.
Example 2:
For synchronously appending
var fs = require('fs');
var data = "\nLearn Node.js";
// Append data to file
fs.appendFileSync('input.txt', data, 'utf8');
console.log("Data is appended to file successfully.")
Output:
Data is appended to file successfully.
Before Appending Data to input.txt file:
GeekyExpert: A computer science portal
After Appending Data to input.txt file:
GeekyExpert: A computer science portal
Learn Node.js
Closing the File:
The fs.close() method is used to asynchronously close the given file descriptor thereby clearing the file that is associated with it. This will allow the file descriptor to be reused for other files. Calling fs.close() on a file descriptor while some other process is being performed on it may lead to undefined behavior.
Syntex:
fs.close(fd, callback)
Parameters:
- fd: It is an integer that denotes the file descriptor of the file for which to be closed.
- callback: It is a function that would be called when the method is executed.
- err: It is an error that would be thrown if the method fails.
Example:
Let us create a js file named main.js having the following code:
// Close the opened file.
fs.close(fd, function(err) {
if (err) {
console.log(err);
}
console.log("File closed successfully.");
}
Output:
File closed successfully.
Delete a File:
The fs.unlink() method is used to remove a file or symbolic link from the filesystem. This function does not work on directories, therefore it is recommended to use fs.rmdir() to remove a directory.
Syntex:
fs.unlink(path, callback)
Parameters:
- path: It is a string, Buffer, or URL which represents the file or symbolic link that has to be removed.
- callback: It is a function that would be called when the method is executed.
- err: It is an error that would be thrown if the method fails.
Example:
Let us create a js file named main.js having the following code:
var fs = require("fs");
console.log("deleting an existing file");
fs.unlink('input.txt', function(err) {
if (err) {
return console.error(err);
}
console.log("File deleted successfully!");
});
Output:
deleting an existing file
File deleted successfully!