This is the 14th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge.

Fs is a built-in core module of NodeJs that provides apis for filesystem operations

Basic operation class

  • Fs.stat () gets directory and file information

    Note: It is not recommended to use fs.stat() to check whether the file exists before calling fs.open(), fs.readfile (), or fs.writefile (). Instead, the user code should directly open/read/write to the file and handle the errors raised if the file is unavailable.

  • Fs.write () writes the file in the buffer to the disk file

    Note: It is not safe to use fs.write() multiple times on the same file without waiting for a callback. In this case, it is recommended to use fs.createWritestream ()

  • Fs.read () writes data from a disk file to a buffer that needs to be read in the fs.open() callback

Commonly used API

Note: error priority is given to callbacks

  • Permission to operate
    • Chmod changes the permissions of the file
  • Directory operations
    • Accesss: checks whether the file or directory has operation permission

      Do not use fs.access() to check file accessibility before calling fs.open(), fs.readfile (), or fs.writefile ()

    • Stat: Obtain directory and file information
    • Mkdir: creates a directory
    • Rmdir: deletes a directory
    • Readdir: reads the contents of the directory
    • Unlink: deletes the specified file
  • Add, delete, modify and check documents
    • Turn on/Off
      • Open () Opens the file
      • Close () Closes the file
    • Read readFile: Reads data from the specified file
    • write
      • WriteFile: writes data to a specified file. If the path does not exist, it is created directly. The third parameter can be set to mode, flag, and encoding
      • AppendFile: adds data to the specified file
      • CopyFile: Copies data from one file to another
    • Delete RM asynchronously deletes files and directories
    • WatchFile: monitors the specified file

supplement

  • Flag flags
    • R: can read
    • W: to write
    • S: synchronous
    • + : Performs the opposite operation. R + means that the opposite operation of adding r is writable, so R + means both readable and writable
    • X: Exclusive operation
    • A: The operation is appended
    • Ax: Similar to a but fails if path exists
    • A +: opens the file for reading and appending. If the file does not exist, it is created.