info@enlightsolution.com           01710-318199           9:00AM - 6:00PM

  • Home
  • Services
    • Custom Software Development
    • Web Application
    • Mobile Application
  • Products
    • Enlight Business Solutions
    • Enlight Accounting
    • Enlight HR & Payroll
    • Enlight POS
    • Asset Management System
  • Portfolio
  • Insights
  • About Us
  • Career
  • Contact US

Node.js File System Module

Node.js File System Module

by Admin / Sunday, 30 June 2019 / Published in JavaScript, Software Development

File System Module:

The Node.js file system module allows you to work with the file system on your computer. It exports functions for working with files and directories. The fs module is responsible for all the asynchronous or synchronous file I/O operations.

To include the File System module, use the require() method:

we can perform read, write, delete and many more operations. Let us look at some of the most important operations.

Reading Files Synchronously and Asynchronously

The fs module provides simple methods to read files:  fs.readFile() and fs.readFileSync(). The fs.readFile() is for reading file asynchronously and fs.readFileSync() is for reading files synchronously.

Here, file path has to be provided as a first argument and a callback function that will be called with file data.

For fs.readFileSync(), file path has to be provided as a first argument. utf8 is the default encoding, but we can specify any custom encoding that we need as the second parameter in both the methods.

Writing Files Synchronously and Asynchronously

Create a new file using the fs.writeFile() and fs.writeFileSync() method. The fs.writeFile() is for reading file asynchronously and fs.writeFileSync() is for reading files synchronously.

In the synchronous write operation

Deleting a File

To delete a file with the File System module, use the fs.unlink() method.

Renaming a File

A file can be renamed using fs.rename(). Here, it takes old file name, new file name and a callback function as arguments.

About Admin

What you can read next

Array, Tuple, Union in TypeScript
HTTP Module
Streams in Node.js

Leave a Reply Cancel reply

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

ENLIGHT SOLUTIONS

Enlight Solutions is working with Leading software development system. We provide out clients nice and fluent software solutions in minimum amount of time.

Quick Links

  • About Us
  • Products
  • Services
  • Portfolio
  • Blog
  • Career

All right reserved.
ENLIGHT SOLUTIONS @ 2019

  • Home
  • contact us
TOP