Node.js from getting started to giving up

Using fs module

import fs from 'fs'

// Common methods of fs module
fs.writeFile()    // write file
fs.appendFile()	  // Write file for append
fs.readFile()	  // Read the file, and the read data is of buffer type
fs.unlink()		  // Delete file
fs.rename()		  // Rename or cut files
fs.stat()	      // Determine file or directory
fs.readdir()	  // Gets the list of file names in the specified directory
fs.mkdir()		  // Create directory
fs.rmdir()		  // Delete directory

1. writeFile method

Parameter 1: the name of the file to be written. You can specify a specific path. If you specify a specific path, the file directory of the path must exist, otherwise an error will be reported
Parameter 2: write the contents of the file
Parameter 3: callback function
After calling this method, an index.txt file will be created in the current directory. If the file already exists, delete the file and re create it. The content of the file is: Node.js from getting started to giving up.

fs.writeFile('index.txt', 'Node.js From getting started to giving up', err => {
    if (err) {
        console.log(err)
        return
    }
    console.log('Write file succeeded!')
})

2. appendFile method

Parameter 1: the name of the file to be written. You can specify a specific path. If you specify a specific path, the file directory of the path must exist, otherwise an error will be reported
Parameter 2: write the contents of the file
Parameter 3: callback function
After calling this method, an index.txt file will be created in the current directory. If the file already exists, the file will not be deleted, but content will be added on the basis of the original file. The content of the file is: Node.js from getting started to giving up. Call twice, and the contents of the file are: Node.js from getting started to giving up, Node.js from getting started to giving up.

fs.appendFile('index.txt', 'Node.js From getting started to giving up', err => {
    if (err) {
        console.log(err)
        return
    }
    console.log('The file of additional content was written successfully!')
})

3. readFile method

Parameter 1: Specifies the file to read. If the file does not exist, an error will be reported
Parameter 2: callback function
After calling this method, the specified file content will be read and returned by the second parameter of the callback function. The type is buffer. Call the toString method to display the printed log content on the console.

fs.readFile('index.txt', (err, data) => {
        if (err) {
        console.log(err)
        return
    }
    console.log('Read file successfully!', data.toString())
})

4. unlink method

Parameter 1: Specifies the file to delete. If the file does not exist, an error will be reported
Parameter 2: callback function
When this method is called, the specified file is deleted.

fs.unlink('index.txt', err => {
    if (err) {
        console.log(err)
        return
    }
    console.log('Delete file succeeded!')
})

5. rename method

Parameter 1: old file name
Parameter 2: new file name
Parameter 3: callback function
After calling this method, if the old and new files are in the same directory, rename the file name; if the old and new files are not in the same directory, cut the file. If the old file does not exist, an error will be reported; When specifying a specific directory, an error is reported if the directory does not exist.

fs.rename('index.txt', 'news/css.txt', err => {
    if (err) {
        console.log(err)
        return
    }
    console.log('Rename file succeeded!')
})

6. stat method

Parameter 1: specified file or directory
Parameter 3: callback function
Call this method to judge the file or directory. If the specified file or directory does not exist, an error will be reported.

fs.stat('index.txt', (err, stats) => {
    if (err) {
        console.log(err)
        return
    }
    console.log('File:', stats.isFile())		// File: true
    console.log('catalog:', stats.isDirectory())	// Directory: false
})

7. readdir method

Parameter 1: the specified directory can specify a specific path. If a specific path is specified, the file directory of the path must exist, otherwise an error will be reported
Parameter 2: callback function
Call this method to create the specified directory.

fs.readdir('news', (err, data) => {
    if (err) {
        console.log(err)
        return
    }
    console.log('Read directory successfully!', data)		// Read directory successfully! [news.text, new.css, other]
})

8. mkdir method

Parameter 1: specified directory name
Parameter 2: callback function
Call this method to create the specified directory.

fs.mkdir('news', err => {
        if (err) {
        console.log(err)
        return
    }
    console.log('Folder created successfully!')
})

9. rmdir method

Parameter 1: specified directory
Parameter 2: callback function
Call this method to delete the specified directory. If the specified directory does not exist or there are contents in the specified directory, an error will be reported.

fs.rmdir('t', err => {
        if (err) {
        console.log(err)
        return
    }
    console.log('Folder deleted successfully!')
})

Keywords: node.js

Added by atticus on Mon, 20 Sep 2021 13:40:00 +0300