Home > Web Front-end > JS Tutorial > An article to talk about the reading and writing operations of node files

An article to talk about the reading and writing operations of node files

青灯夜游
Release: 2022-12-19 19:18:47
forward
2612 people have browsed it

This article will talk about the fs file system module and introduce the file reading and writing operations in node. I hope it will be helpful to everyone!

An article to talk about the reading and writing operations of node files

fs file system module

##What is fs file system module

fs module is a module officially provided by node.js for operating files. Reading and writing operations on files can be achieved through the fs module. [Related tutorial recommendations:

nodejs video tutorial, Programming teaching]

For example:

    fs.readFile(): used for reading Get the file content of the specified file
  • fs.writeFile(): used to write content to the specified file
Introduce the fs module:

const fs = require('fs');

Read the contents of the specified file

    fs.readFile() Syntax:
  • fs.readFile(path[,options],callback)
  • Parameters:
    • path: required parameter, string format, indicating the path of the file
    • options:
    • Optional Parameters, indicating the encoding format to read the file
    • Callback: Required parameter: After the file reading is completed, return to reading through this callback function The result (failure: failure information; success: read result)
Chestnut:

First I create a file named

test. txt document, and the content inside is: 12341234

Then we use the fs module in node.js to read the file:

     // 引入fs模块
     const fs = require('fs');
     
     // 读取文件
     fs.readFile('./test.txt','utf-8',function(err,data){
         console.log(err);// null
         console.log(data);// 12341234
     })
Copy after login

We can See that when reading the file, there are two parameters in the callback function:

  • The first parameter represents the parameter that failed to read. At this time, we read successfully here. So the result is null

  • The second parameter represents the result after successful reading. Here we read the content of the file, so the output is the content of the file.

We can judge whether the file is read successfully based on the value returned by the first parameter of the read file callback function: if null is returned, it means the file is read successfully; otherwise, the file is read successfully. fail.

Write content to the specified file

    fs.writeFile() syntax:
  • fs.writeFile(file,data [,options],callback);
  • Parameters:
    • Parameter 1: Required parameter, string format, indicating the path of the file
    • Parameter 2: Required Select parameters, indicating the content to be written
    • Parameter 3:
    • Optional Parameter, indicating the encoding format to write the content in
    • Parameter 4: Required parameter, file writing After entering the callback function
Chestnut:

     const fs = require('fs');
 
     fs.writeFile('text.txt', '海绵宝宝', 'utf-8', function(err) {
         console.log(err);// null
     })
Copy after login

After this code is executed, a

null will be output. Is that right? Does it mean that the writing has been successful?

Yes, in the same level folder, we can see that a new

text.txt file has been generated. Open it and find that the SpongeBob SquarePants# we just wrote is official. ##. So if we execute the code again and only the written content changes, what will be the result?

 fs.writeFile('text.txt', '派大星', 'utf-8', function(err) {
         console.log(err);// null
     })
Copy after login

At this time, we open the

text.txt

file again and find that the content inside becomes Pat Star, which means using wirteFile() will overwrite the original content of the file. At this time, we can also judge whether the file is written successfully based on the value returned by the parameter of the write file callback function: if null is returned, it means the file is written successfully; otherwise, the writing fails.

EndThrough the fs module of

node.js

, we can read and write files Come on, this article is my study notes for learning node.js. If there are any shortcomings, I hope the experts can give me advice. For more node-related knowledge, please visit:

nodejs tutorial

!

The above is the detailed content of An article to talk about the reading and writing operations of node files. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template