How to use module fs file system in Nodejs

How to use module fs file system in Nodejs

Overview

Node's fs documentation contains a lot of APIs, after all, it fully supports file system operations. The documentation is well organized, and the operations are basically divided into file operations, directory operations, file information, and streams. The programming method also supports synchronization, asynchrony, and Promise.

This article records several issues that are not described in detail in the document, which can better connect the fs document ideas:

  • File Descriptors
  • Synchronous, asynchronous and Promise
  • Catalogs and Catalog Items
  • File information
  • stream

File Descriptors

A file descriptor is a non-negative integer. It is an index value that the operating system can use to find the corresponding file.

In many low-level APIs of fs, file descriptors are required. In the documentation, descriptors are usually represented by fd. For example: fs.read(fd, buffer, offset, length, position, callback). The corresponding api is: fs.readFile(path[, options], callback).

Because the operating system has a limit on the number of file descriptors, don't forget to close the file after completing the file operation:

const fs = require("fs");

fs.open("./db.json", "r", (err, fd) => {
    if (err) throw err;
    // File operations...
    // After completing the operation, close the file fs.close(fd, err => {
        if (err) throw err;
    });
});

Synchronous, asynchronous and Promise

All file system APIs have both synchronous and asynchronous forms.

Synchronous writing

It is not recommended to use synchronous APIs, as they will block the thread.

try {
    const buf = fs.readFileSync("./package.json");
    console.log(buf.toString("utf8"));
} catch (error) {
    console.log(error.message);
}

Asynchronous writing

Asynchronous writing is easy to fall into callback hell.

fs.readFile("./package.json", (err, data) => {
    if (err) throw err;
    console.log(data.toString("utf8"));
});

(Recommended) Promise writing

Before node v12, you need to use promise encapsulation yourself:

function readFilePromise(path, encoding = "utf8") {
    const promise = new Promise((resolve, reject) => {
        fs.readFile(path, (err, data) => {
            if (err) return reject(err);
            return resolve(data.toString(encoding));
        });
    });
    return promise;
}

readFilePromise("./package.json").then(res => console.log(res));

In node v12, the fs Promise api was introduced. They return Promise objects instead of using callbacks. The API is accessible via require('fs').promises. This reduces development costs.

const fsPromises = require("fs").promises;

fsPromises
    .readFile("./package.json", {
        encoding: "utf8",
        flag: "r"
    })
    .then(console.log)
    .catch(console.error);

Catalogs and Catalog Items

fs.Dir class: encapsulates operations related to file directories

fs.Dirent class: encapsulates operations related to directory entries. For example, determine the device type (character, block, FIFO, etc.).

The relationship between them is shown in code:

const fsPromises = require("fs").promises;

async function main() {
    const dir = await fsPromises.opendir(".");
    let dirent = null;
    while ((dirent = await dir.read()) !== null) {
        console.log(dirent.name);
    }
}

main();

File information

fs.Stats class: encapsulates operations related to file information. It is returned in the fs.stat() callback function.

fs.stat("./package.json", (err, stats) => {
    if (err) throw err;
    console.log(stats);
});

Note, about checking if the file exists:

  • It is not recommended to use fs.stat() to check if a file exists before calling fs.open(), fs.readFile(), or fs.writeFile(). Instead, you should open and read or write the file directly, and handle any errors that occur if the file is not available.
  • To check if a file exists but not subsequently operate on it, it is recommended to use fs.access()

ReadStream and WriteStream

In nodejs, stream is a very important library. The APIs of many libraries are encapsulated based on streams. For example, the ReadStream and WriteStream in fs described below.

fs itself provides readFile and writeFile, but the price of their usefulness is performance issues, as all the content will be loaded into memory at once. But for large files of several GB, there will obviously be problems.

So the solution for large files is naturally: read them out bit by bit. This requires the use of stream. Taking readStream as an example, the code is as follows:

const rs = fs.createReadStream("./package.json");
let content = "";

rs.on("open", () => {
    console.log("start to read");
});

rs.on("data", chunk => {
    content += chunk.toString("utf8");
});

rs.on("close", () => {
    console.log("finish read, content is:\n", content);
});

With the help of stream pipe, a large file copy function can be quickly encapsulated in one line:

function copyBigFile(src, target) {
    fs.createReadStream(src).pipe(fs.createWriteStream(target));
}

The above is the details of how to use the module fs file system in Nodejs. For more information about Nodejs, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • node.js-fs file system module is this you know
  • Detailed example of using the fs file system module in node.js
  • Detailed explanation of node.js file operating system example
  • Detailed explanation of how to use the NodeJs file system operation module fs
  • How much do you know about Node's file system?

<<:  C# implements MySQL command line backup and recovery

>>:  Ansible automated operation and maintenance deployment method for Linux system

Recommend

Tomcat+Mysql high concurrency configuration optimization explanation

1.Tomcat Optimization Configuration (1) Change To...

Native js implementation of magnifying glass component

This article example shares the specific code for...

js realizes packaging multiple pictures into zip

Table of contents 1. Import files 2. HTML page 3....

Comparison of mydumper and mysqldump in mysql

If you only want to back up a few tables or a sin...

A simple method to modify the size of Nginx uploaded files

Original link: https://vien.tech/article/138 Pref...

Several ways to store images in MySQL database

Usually the pictures uploaded by users need to be...

A brief discussion on mysql backup and restore for a single table

A. Installation of MySQL backup tool xtrabackup 1...

A brief analysis of the game kimono memo problem

Today, after the game was restarted, I found that...

MySQL 5.7 installation and configuration tutorial under CentOS7 (YUM)

Installation environment: CentOS7 64-bit, MySQL5....

Linux uses lsof command to check file opening status

Preface We all know that in Linux, "everythi...

jQuery plugin to implement dashboard

The jquery plug-in implements the dashboard for y...

Comparison of CSS shadow effects: drop-Shadow and box-Shadow

Drop-shadow and box-shadow are both CSS propertie...