Node.js Tutorial

Node.js File System (FS)

Node.js File System (FS)
Menu

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:

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.

Lets look at an example.

Data Science Tutorial:-

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 and fd (file descriptor).

Machine Learning Tutorial:

Common File Open Flags

Flag Description
r Open file for reading. Throws an error if the file doesnt exist.
r+ Open file for reading and writing. Throws an error if the file doesnt 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 doesnt 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 doesnt exist.
ax Like a but fails if the file exists.
a+ Open file for reading and appending. Creates the file if it doesnt exist.
ax+ Like a+ but fails if the file exists.

Heres an example of opening a file asynchronously:

Complete Advance AI topics:- 

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 and stats. The stats object is an instance of fs.Stats and provides various file properties and methods.

Deep Learning Tutorial: 

Common fs.Stats Methods

Method Description
stats.isFile() Returns true if the file is a regular file.
stats.isDirectory() Returns true if its a directory.
stats.isBlockDevice() Returns true if its a block device.
stats.isCharacterDevice() Returns true if its a character device.
stats.isSymbolicLink() Returns true if its a symbolic link.
stats.isFIFO() Returns true if its a FIFO.
stats.isSocket() Returns true if its 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:-
SQL Tutorial :


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

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us