Node.js File System (FS)
Node.js File System
In Node.js, file I/O operations are built upon simple wrappers around standard POSIX functions. The File System (fs) module in Node.js enables developers to work with files easily — from reading and writing to updating and deleting files.
Introduction to Applied AI:–Click Here
To use the File System module, you can import it using the following syntax:
var fs = require("fs");
Node.js FS – Reading a File
Every method in the fs
module has both synchronous and asynchronous versions.
- Asynchronous methods take a callback function as the last parameter and do not block the event loop, allowing other code to execute while the file operation is in progress.
- Synchronous methods, on the other hand, block program execution until the operation completes.
The asynchronous approach is generally preferred for better performance and scalability.
Let’s look at an example.
Data Science Tutorial:-Click Here
Create a text file named input.txt with the following content:
File: input.txt
UpdateGadh is one of the best online tutorial websites to learn different technologies
in a very easy and efficient manner.
Now, create a JavaScript file named main.js with the following code:
File: main.js
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());
});
// Synchronous read
var data = fs.readFileSync('input.txt');
console.log("Synchronous read: " + data.toString());
console.log("Program Ended");
Run the code in the Node.js terminal using the command:
Download New Real Time Projects :–Click here
node main.js
Node.js – Opening a File
You can open a file in asynchronous mode using the following syntax:
fs.open(path, flags[, mode], callback)
Parameters:
- path: The name or path of the file to be opened.
- flags: Specifies how the file should be opened (read, write, append, etc.).
- mode: Defines the file permissions (default is
0666
, which allows read and write). - callback: A function that receives two arguments —
err
andfd
(file descriptor).
Machine Learning Tutorial:–Click Here
Common File Open Flags
Flag | Description |
---|---|
r |
Open file for reading. Throws an error if the file doesn’t exist. |
r+ |
Open file for reading and writing. Throws an error if the file doesn’t exist. |
rs |
Open file for reading in synchronous mode. |
rs+ |
Open file for reading and writing in synchronous mode. |
w |
Open file for writing. Creates the file if it doesn’t exist, or truncates it if it does. |
wx |
Like w but fails if the file already exists. |
w+ |
Open file for reading and writing. Creates or truncates as needed. |
wx+ |
Like w+ but fails if the file exists. |
a |
Open file for appending. Creates the file if it doesn’t exist. |
ax |
Like a but fails if the file exists. |
a+ |
Open file for reading and appending. Creates the file if it doesn’t exist. |
ax+ |
Like a+ but fails if the file exists. |
Here’s an example of opening a file asynchronously:
Complete Advance AI topics:-Â CLICK HERE
File: main.js
var fs = require("fs");
console.log("Going to open file!");
fs.open('input.txt', 'r+', function(err, fd) {
if (err) {
return console.error(err);
}
console.log("File opened successfully!");
});
Run the program using:
node main.js
Node.js – File Information
You can get detailed information about a file using the fs.stat()
method.
Syntax:
fs.stat(path, callback)
Parameters:
- path: The name or path of the file.
- callback: Function with two parameters —
err
andstats
. Thestats
object is an instance offs.Stats
and provides various file properties and methods.
Deep Learning Tutorial:– Click Here
Common fs.Stats
Methods
Method | Description |
---|---|
stats.isFile() |
Returns true if the file is a regular file. |
stats.isDirectory() |
Returns true if it’s a directory. |
stats.isBlockDevice() |
Returns true if it’s a block device. |
stats.isCharacterDevice() |
Returns true if it’s a character device. |
stats.isSymbolicLink() |
Returns true if it’s a symbolic link. |
stats.isFIFO() |
Returns true if it’s a FIFO. |
stats.isSocket() |
Returns true if it’s a socket. |
Example:
File: main.js
var fs = require("fs");
console.log("Going to get file info!");
fs.stat('input.txt', function (err, stats) {
if (err) {
return console.error(err);
}
console.log(stats);
console.log("Got file info successfully!");
console.log("isFile ? " + stats.isFile());
console.log("isDirectory ? " + stats.isDirectory());
});
Run the script using:
node main.js
Complete Python Course with Advance topics:-Click Here
SQL Tutorial :–Click Here
fs – npm path module in node js fs.writefile in node js read file in node js fs/promises node fs readfile node:fs/promises how to create a file in node js command prompt node js file system examples node js file system w3schools node js file system tutorial nodejs file system, nodejs file system module, nodejs file system tutorial, file system module nodejs, node.js file system, node js file system, node.js file system tutorial, node.js file system examples, file system node js, node js files system, node file system, node js file system module, nodejs filesystem, file system, nodejs file, file system module in node js, nodejs filesystem walk, nodejs filesystem module, file system module, read file system, filesystem nodejs module, nodejs file read write
Post Comment