Home >Web Front-end >JS Tutorial >Node file system: fs asynchronous and synchronous (file reading and writing)

Node file system: fs asynchronous and synchronous (file reading and writing)

藏色散人
藏色散人forward
2022-08-08 15:11:033818browse

Synchronization and asynchronous

The ones with Sync in fs are synchronous and those without are asynchronous

Let’s first distinguish between synchronization and asynchronousness

Synchronous: The previous code is executed first, and the following code needs to wait for the previous code to be executed before it is executed.

Asynchronous: The code is executed in no particular order, which means that the execution of the previous code will not cause the subsequent code to block. Therefore, the order of execution results of asynchronous code is not certain.

There are two ways to operate files in fs, asynchronous and synchronous. Asynchronous is divided into ordinary asynchronous and Promise asynchronous. See the code for details.

The value and meaning of flag in options

r: 读取文件,文件不存在则报错
r+:读取并写入文件,如果文件不存在则报错
rs:以同步的方式读取文件并通知操作系忽略本地文件系统缓存。(一般不用)
w:写入文件。如果文件不存在则创建该文件,如果文件存在则覆盖
wx:作用和w类似,如果路径已存在则失败。
w+:读取并写入文件。如果文件不存在则创建该文件,如果文件存在则覆盖
wx+:和w+类型,如果路径已存在则失败。
a:追加写入文件,如果文件不存在则创建文件
ax:作用和a类型,如果路径已存在则失败。
a+: 读取并追加写入文件,如果文件不存在则创建文件
ax+:作用和a+类似,如果路径已存在则失败。

Import the module before use

let fs=require('fs');

1. Read the file

1 .readFileSync(path[, options]) Synchronously read files

path: File path

options: Optional parameters used to configure the read file options are the same

//  同步读取 需要使用一个变量来接收读取出来的数据
let data=fs.readFileSync(path,{  // path为文件的路径
		encoding:'utf8',  // 指定字符集
		flag:'r'		  // 指定读取的模式  具体上面有
});	
console.log(data.toString());   // 默认读出来的是buffer类型 使用toString()转为字符串

2. readFile(path,[,options],callback(error,data)) Normally read the file asynchronously

// 普通异步读取不需要变量 直接在回调函数中读取数据  需要注意的是:
// 回调函数接收两个参数 第一个是error 也就是异常 说明文件读取失败 如果error为null 则读取成功 data即数据
fs.readFile(path,{encoding:'utf8',flag:'r'},function(error,data){
    console.log(data.toString())
});

3. fs.promises.readFile (path[, options]) Read the file in promise mode

	// fs.promises.xxx 返回的是一个promise的对象 需要学习promise的语法 then()接收一个参数data 即数据
fs.promises.readFile(path).then(data=>{
   	console.log(data.toString());
})
// 或
let fsPromise=fs.promises.readFile(path);
fsPromise.then(data=>{
	console.log(data.toString());
})

2. Write the file

The effect of appendFile is the same as flag:'a ' in writeFile

path: The file path data is the written data option as above

1. fs.writeFileSync(file, data[, options]) Synchronously writes the file

// data为需要写入的数据 options同上 写入的方式 a+为追加写入方式
fs.writeFileSync(path,data,{flag:'a+'}); // 返回值为undefined

2. fs. appendFileSync(file, data[, options]) Synchronous append mode to write files

fs.appendFileSync(path,data,{});  // 返回值为undefined

3. fs.writeFile(file, data[, options],callback) Ordinary asynchronous writing

fs.writeFile(path,data,{flag:'a+'},function(error){
	if(err){
		console.log("写入失败");
	}else{
		console.log("写入成功");
	}})

4. fs.promises.writeFile(file, data[, options]) Promise is written asynchronously

fs.promises.writeFile(path,data,{flag:'a+'});  // 写入操作 没有返回值 也就不需要then了

5. fs.appendFile(path, data[, options], callback) Asynchronous append is written to the file

fs.appendFile(path,appendData,function(){		})

6. fs.promises.appendFile(path, data[, options]) Promise method to append written files

fs.promises.writeFile(path,data);

[Recommended: node.js video tutorial

The above is the detailed content of Node file system: fs asynchronous and synchronous (file reading and writing). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete