


An article to talk about the reading and writing operations of node files
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!
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
const fs = require('fs');
Read the contents of the specified file
- fs.readFile() Syntax:
- fs.readFile(path[,options],callback)
- 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)
test. txt document, and the content inside is:
12341234
// 引入fs模块 const fs = require('fs'); // 读取文件 fs.readFile('./test.txt','utf-8',function(err,data){ console.log(err);// null console.log(data);// 12341234 })
- 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.
Write content to the specified file
- fs.writeFile() syntax:
- fs.writeFile(file,data [,options],callback);
- Parameter 1: Required parameter, string format, indicating the path of the fileParameter 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
const fs = require('fs'); fs.writeFile('text.txt', '海绵宝宝', 'utf-8', function(err) { console.log(err);// null })
null will be output. Is that right? Does it mean that the writing has been successful?
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 })
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:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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!

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 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!

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.

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!

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

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!

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
