Node fs file system module usage details _node.js_ Script home

This section describes how to use the fs file system module in Node

Updated: March 19, 2024 10:12:25 Author: Fusu 1002
fs module is the official Node.js, used to operate the file module (built-in api), it provides a series of methods and attributes, used to meet the user's file operation needs, this article introduces the use of Node fs file system module, need friends can refer to the next

What is the fs file system module

  • The fs module is an official Node.js module for manipulating files (built-in api).
  • It provides a series of methods and properties to meet the user's requirements for manipulating files

For example:

  • fs.readFile()Method to read the contents of the specified file
  • fs.writeFile()Method to write content to the specified file

Second, the use of fs module

If you want to use the fs module to manipulate a file in JavaScript code, you need to import it first as follows

const fs = require("fs");

2.1.fs.readFile () syntax format

Using the fs.readFile() method, you can read the contents of a specified file in the following syntax:

fs.readFile(path,[, options],callback)

Parameter interpretation

  • Parameter 1: Mandatory. It is a character string and indicates the file path
  • Parameter 2: Optional parameter indicating the encoding format in which the file is read.
  • Parameter 3: Mandatory parameter, after the file is read, through the callback function to get the read result (if there is no parameter 2, then parameter 3 can be directly written to the position of parameter 2)

image.png

// Import file system module const fs = require("fs"); fs.readFile("./ achieve.txt ", "utf8", function (err, dataStr) {if (err) return console.log(err.message); // Failed to read console.log(dataStr). // Read successfully});

image.png

2.2. Syntax format of fs.writeFile()

Using the fs.writeFile() method, you can write to a specified file in the following syntax:

fs.writeFile(path,data,[, options], callback)

Look outfs.writeFileTwo major features

  • If the file directory to write to exists, but the file name does not, it will help you create a file and write to it
  • If a file is written twice at the same time, the file content is not appended but replaced directly

Parameter interpretation

  • Parameter 1: This parameter is mandatory. It is a character string indicating the file path
  • Parameter 2: specifies the content to be written. This parameter is mandatory
  • Parameter 3: Optional parameter, indicating the format in which the file content is written, the default value is utf8 Parameter 4: mandatory parameter, after the file is written, through the callback function to get the write result (if there is no parameter 3, then parameter 4 can be directly written to the position of parameter 3)

image.png

// Import file system module const fs = require("fs"); fs.writeFile("./hello.txt", "hello Nodejs", function (err) { if (err) return console.log(err.message); // Write to console.log(" Write succeeded "); // Write successfully});

image.png

image.png

3. Practice: Sorting out test scores

Core implementation step

  • Import requiredfs File system module
  • Usefs.readFile()Method, read the material directoryGrade.txtdocument
  • Check whether the file failed to be read
  • After the file is successfully read, the score data is processed
  • Will process the completed grade data, call fs.writeFile()Method, write toNew grade.txtIn the
  • The effect is as follows

index.js implementation code

// Import file operation module const fs = require("fs"); readFile("./ achieve.txt ", "utf8", (error, dataStr) => {if (error) return console.log(" file read failed ", error.message); const oldArray = dataStr.split(" "); / / [little red = 80, xiao LAN = 60] let newArray = oldArray. The map ((item) = > item. Replace (" = ", ":")); //[Xiao Hong: 80, Xiao LAN: 60] let newStr = newArray.join("\r\n"); fs.writeFile("./ new score.txt ", newStr, "utf8", (error) => {if (error) return console.log(" File write failed ", error); console.log(" File written successfully!! ") ); }); });

4. Dynamic path splicing of fs file system module in Nodejs

The problem and solution of dynamic path concatenation of fs file system module in Nodejs _Node.js_Script Home (jb51.net)

To this article about the use of fs file system module in Node detailed explanation of the article is introduced to this, more related Node fs file system module content please search script home previous articles or continue to browse the following related articles hope that you will support script home in the future!

Related article

Latest comments