Fs module in Node

Node.js file system (FS module) Methods in the module are both asynchronous and synchronous versions, such as fs.readfile () and synchronous fs.readfilesync () for reading file contents.

The last parameter of an asynchronous method function is a callback function. The first parameter of the callback function contains an error message.

It is recommended that you use asynchronous methods, which have higher performance, are faster, and do not block compared to synchronous methods.

If the synchronization method fails, the execution fails.

The fs module needs to be introduced first

const fs = require('fs')

Check whether the directory exists

    const stats = fs.statSync('./index.js') // Stats
    fs.stat('./index.js'.function (error, state) {
        error // Error message
        state // Stats
    })
    
    Stats {
      dev: 3472390110.// The numeric identifier of the device containing the file.
      mode: 33206.// Bitfields describing file types and modes.
      nlink: 1.// The number of hard links in the file.
      uid: 0.// The numeric user identifier (POSIX) of the user who owns the file.
      gid: 0.// The numeric group identifier (POSIX) of the group that owns the file.
      rdev: 0.// A digital device identifier if the file represents a device.
      blksize: 4096.// File system block size for I/O operations.
      ino: 12384898978083840.// The file system specific index node number of the file.
      size: 8826.// Size of the file in bytes.
      blocks: 24.// The number of blocks allocated for this file.
      atimeMs: 1629084741808.1633.// Indicates the timestamp of the last access to this file, in milliseconds since POSIX Epoch.
      mtimeMs: 1629084732153.9712.// Indicates the last time the timestamp of this file was modified, in milliseconds since POSIX Epoch.
      ctimeMs: 1629084732153.9712.// Indicates the timestamp of the last file state change, expressed in milliseconds since POSIX Epoch.
      birthtimeMs: 1606380451997.3667.// Timestamp indicating when this file was created, in milliseconds since POSIX Epoch.
      atime: 2021-08-16T03:32:21.808Z, // Indicates the last time the file was accessed.
      mtime: 2021-08-16T03:32:12.154Z, // indicates the last time the timestamp of this file was modified.
      ctime: 2021-08-16T03:32:12.154Z, // Indicates the last time the file state was changed.
      birthtime: 2020-11-26T08:47:31.997Z // A timestamp indicating when this file was created.
    }
    
    Stats.isDirectory() // Returns true if the 
      
        object describes the file system directory.
      
    Stats.isFile() Returns true if the 
      
        object describes a regular file.
      
Copy the code

Read the directory

Reading directories must ensure that the current path is a folder path and not a file

    const fileList = fs.readdirSync('./fileList') // File directory
    fs.readdir(dirPath, function (error, fileList) {
        error // Error message
        fileList // File directory
    })
    
    fileList ['.git', 'index.js']
Copy the code

Read the file

To read a file, ensure that the file in the current path exists

The second argument is string which means open in what file code

    fs.readFileSync('./index.js'.'utf-8')
    fs.readFile('./index.js'.'utf-8'.function (error, data) {
        error // Error message
        data // File contents
    })
Copy the code

Delete the file

To delete a file, ensure that the current path is a file

    fs.unlinkSync('./index.js')
    fs.unlink('./index.js'.function (error) {
        error // Error message
    })
    
    // No error message will be displayed if the deletion succeeds
Copy the code

Delete folders

To delete a folder, ensure that the current path is a folder

    fs.rmdirSync('./fileList')
    fs.rmdir('./fileList'.function (error) {
        error // Error message
    })
    
    // No error message will be displayed if the deletion succeeds
Copy the code

Creating a folder

To create a folder, ensure that all parent paths of the current path exist

    fs.mkdirSync('./fileList')
    fs.mkdir('./fileList'.function (error) {
        error // Error message
    })
    
    // No error message will be displayed if the creation succeeds
Copy the code

Create a file

To create a file, ensure that all the parents of the current path exist

    const data = 'File contents'
    
    fs.writeFileSync('./index.js', data)
    fs.writeFile('./fileList', data, function (error) {
        error // Error message
    })
    
    // No error message will be displayed if the creation succeeds
Copy the code

Additional documents

To append a file, you add a piece of data to the end of the file

To append a file, ensure that the file in the current path exists

    const data = 'File contents to append'
    
    fs.appendFileSync('./index.js', data)
    fs.appendFile('./fileList', data, function (error) {
        error // Error message
    })
    
    // No error message will be displayed if the creation succeeds
Copy the code

Copy files

The first parameter is the current file path

The second parameter is the path to the file you want to copy to (you can change the file name)

To copy files, ensure that the files in the current path and all parent paths to be copied to the path exist

    fs.copyFileSync('./index.js'.'./index_copy.js')
    fs.copyFile('./index.js'.'./index_copy.js'.function (error) {
        error // Error message
    })
    
    // No error message will be displayed if the copy succeeds
Copy the code

Third-party libraries

yhl-explorer-js – npm (npmjs.com)