Table of Contents
fs file system module
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

Dec 19, 2022 pm 07:18 PM
node.js node

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed graphic explanation of the memory and GC of the Node V8 engine Detailed graphic explanation of the memory and GC of the Node V8 engine Mar 29, 2023 pm 06:02 PM

This article will give you an in-depth understanding of the memory and garbage collector (GC) of the NodeJS V8 engine. I hope it will be helpful to you!

An article about memory control in Node An article about memory control in Node Apr 26, 2023 pm 05:37 PM

The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

How to use express to handle file upload in node project How to use express to handle file upload in node project Mar 28, 2023 pm 07:28 PM

How to handle file upload? The following article will introduce to you how to use express to handle file uploads in the node project. I hope it will be helpful to you!

Let's talk in depth about the File module in Node Let's talk in depth about the File module in Node Apr 24, 2023 pm 05:49 PM

The file module is an encapsulation of underlying file operations, such as file reading/writing/opening/closing/delete adding, etc. The biggest feature of the file module is that all methods provide two versions of **synchronous** and **asynchronous**, with Methods with the sync suffix are all synchronization methods, and those without are all heterogeneous methods.

An in-depth analysis of Node's process management tool 'pm2” An in-depth analysis of Node's process management tool 'pm2” Apr 03, 2023 pm 06:02 PM

This article will share with you Node's process management tool "pm2", and talk about why pm2 is needed, how to install and use pm2, I hope it will be helpful to everyone!

Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Mar 05, 2025 pm 05:57 PM

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

Let's talk about the event loop in Node Let's talk about the event loop in Node Apr 11, 2023 pm 07:08 PM

The event loop is a fundamental part of Node.js and enables asynchronous programming by ensuring that the main thread is not blocked. Understanding the event loop is crucial to building efficient applications. The following article will give you an in-depth understanding of the event loop in Node. I hope it will be helpful to you!

Learn more about Buffers in Node Learn more about Buffers in Node Apr 25, 2023 pm 07:49 PM

At the beginning, JS only ran on the browser side. It was easy to process Unicode-encoded strings, but it was difficult to process binary and non-Unicode-encoded strings. And binary is the lowest level data format of the computer, video/audio/program/network package

See all articles