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

How to block and prohibit web crawlers in Nginx server

Every website usually encounters many non-search ...

25 div+css programming tips and tricks

1. The ul tag has a padding value by default in M...

6 inheritance methods of JS advanced ES6

Table of contents 1. Prototype chain inheritance ...

Enterprise-level installation tutorial using LAMP source code

Table of contents LAMP architecture 1.Lamp Introd...

How to migrate the data directory in mysql8.0.20

The default storage directory of mysql is /var/li...

A collection of common uses of HTML meta tags

What is a mata tag The <meta> element provi...

Linux centOS installation JDK and Tomcat tutorial

First download JDK. Here we use jdk-8u181-linux-x...

Linux command line operation Baidu cloud upload and download files

Table of contents 0. Background 1. Installation 2...

How to change the color of the entire row (tr) when the mouse stops in HTML

Use pure CSS to change the background color of a ...

Solution to mysql login warning problem

1. Introduction When we log in to MySQL, we often...

Vue's detailed code for implementing the shuttle box function

Vue - implement the shuttle box function, the eff...