This section describes how to use the fs file system module in Node
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 filefs.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)
// 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});
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 out
fs.writeFile
Two 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)
// 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});
3. Practice: Sorting out test scores
Core implementation step
- Import required
fs File system module
- Use
fs.readFile()
Method, read the material directoryGrade.txt
document - 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.txt
In 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
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
-
Use GruntJS to build a merged compression of a Web program
The previous article showed how to use the concat and uglify commands to merge and compress JavaScript resources. This article covers the consolidation and compression of css resources.2014-06-06 -
Description NodeJs Project The CentOs linux server is deployed online
This article mainly introduces the NodeJs project CentOs linux server online deployment, this article gives you a very detailed introduction, has a certain reference value, the need of friends can refer to2019-09-09 -
nodejs uses express to create a simple web application
This article mainly introduces nodejs using express to create a simple web application related information, need friends can refer to2017-03-03 -
An example of how Node.js uses AngularJS
This article mainly introduces Node.js using AngularJS method example, Xiaobian feel very good, now share to you, also give you a reference. Let's take a look2018-05-05 -
nodejs Advanced (6) - Example of connecting to the MySQL database
This article mainly introduces the nodejs advanced (6) - connect to the MySQL database example, a detailed introduction of NodeJS operation MySQL database, as the most widely used open source database has become our first choice, interested can understand.2017-01-01 -
Example of how Node crawls a large number of files
This article mainly introduces the method of Node climbing to get a large number of files example, the article through the example code is very detailed, for everyone's study or work has a certain reference learning value, the need of friends below with the small series to learn it2019-06-06 -
Instructions for using the console.log method in node.js
This article mainly introduces the node.js console.log method use instructions, this article introduces console.log method description, syntax, receiving parameters, examples and implementation source code, need friends can refer to the next2014-12-12 -
I believe that there must be some small partners have clearly uninstalled node and installed nvm according to the operation, but still can not normally manage node through nvm, this article will introduce you to the nvm management node can not normally switch the node version of the solution, need friends can refer to2024-01-01
-
Use node.js to crawl the JS reference library of the specified ranking site
Recently in learning node.js crawler, because of the powerful asynchronous characteristics of nodejs, so that we can easily to asynchronous high concurrency to climb the website, the following article mainly introduces you about the use of node.js to climb the designated ranking site of the JS reference library related information, need friends can refer to.2017-07-07 -
Node.js gets information about the request submitted by the front-end ajax
This article mainly introduces in detail the Node.js to obtain front-end ajax submitted request information, has a certain reference value, interested partners can refer to it2017-02-02
Latest comments