Node.js 文件系统模块是什么
Node.js是一个开源的JavaScript运行时环境,用于在浏览器之外运行JavaScript。它是后端开发的流行选择。和其他编程语言一样,Node.js也提供了内置的库来与系统或机器进行交互。在Node.js中,我们将这些库称为模块。
模块基本上分为3个类别:
- Node核心模块
- 本地模块
- 第三方模块(如npm模块)
文件系统模块: 文件系统是一个由Node提供的核心模块,用于与文件和文件夹进行交互。它提供了各种事件和方法来访问和操作我们机器上的文件和文件夹。
它提供的一些操作用于操作文件的操作有:
- 创建和删除文件或文件夹
- 访问和重命名文件或文件夹
- 读取、写入和追加文件
- 更改文件或文件夹的权限和所有者
要在程序中使用文件系统模块,您需要引入fs这个Node核心模块:
语法:
const fs = require('fs')
现在,在使用fs模块之前,您需要了解使用fs模块方法有两种方法:
- 使用同步方法
- 使用异步方法
现在问题来了,我们应该选择同步方法还是异步方法。让我们讨论一下fs模块的同步方法和异步方法之间的区别。
同步方法和异步方法:
同步方法 使用的函数是 阻塞式的 。阻塞函数会阻止下一条指令或代码片段的执行,直到当前操作完成。 同步方法 等待 当前操作完成 ,然后继续执行下一条指令。但是,如果当前操作需要很长时间,这会导致问题。
示例: 假设我们的服务器在收到请求后需要创建一个文件,然后向其中写入一些文本,然后再回应给客户端。如果我们使用同步方法来创建和写入文件,那么对于每个请求,我们都会阻塞执行,直到我们完成操作并回应给客户端。如果有很多请求同时到达,我们实际上是在阻塞其他用户,直到我们完成第一个用户的操作。最后一个用户将不得不等很长时间才能得到这个回应,这是不好的。
异步方法 是非阻塞式的,因为它们从不等待当前操作完成。在调用异步方法时,我们必须将回调函数作为参数传递进去。当调用异步函数时,它会被 事件循环 注册或推送到一个队列中,并执行下一行代码。在后台,我们的异步函数执行并在完成时,将我们作为参数传递的回调函数推入回调队列,并在轮到它时执行。
Basically, the catch here is to understand that asynchronous methods do the operation in the background and do not block the
execution of code.

文件系统提供了同步和异步版本的方法。
注意:-如果我们有选择,我们应该始终在我们的代码中使用异步版本的方法。同步方法只应该在顶级代码中使用,因为顶级代码只执行一次,或者只在我们确定操作不会花费很长时间(轻量级操作)的情况下使用。
文件操作:
- 读取文件 – 读取文件的最简单的方法是使用 fs.readFile() 方法。该方法负责打开和关闭文件,并在内存中加载文件内容供我们在程序中使用。
语法:
fs.readFile(path,options,callback);
参数:
- path – 我们要从中读取的文件的名称或路径
- options – 这是一个可选参数,通常我们传递编码,即’utf-8′
- callback – 当我们读取文件时执行,它接受两个参数
- error – 如果发生任何错误
- data – 文件的内容
示例 – 创建一个名为 app.js 的文件,还创建一个名为 input.txt 的文本文件
项目结构 –

app.js
const fs = require("fs");
// Asynchronous version of readFile method
fs.readFile("input.txt", "utf-8", (error, data) => {
// If the file doesnt exist then there
// is error then if condition will run
if (error) {
console.log("File not found");
} else {
// Data contains the content that we
// have read from file in case of
// error , data will output undefined
console.log(data);
}
});
运行 app.js 使用以下命令:
node app.js
输出:
Hurrah, now you know how to read a file.
示例2: 我们将会使用同步版本,即 fs.readFileSync() 。下面讨论的其他方法也都有同步版本。要使用同步版本,只需在函数名后面加上Sync,并移除回调参数。
const fs = require("fs");
try {
// If file not found then throws error
//Reading file in asynchronous way
const data = fs.readFileSync("input.txt", "utf-8");
console.log(data);
} catch (error) {
console.log(error);
}
输出:
Hurrah, now you know how to read a file synchronously.
- 写入文件 - 我们可以使用 fs.writeFile() 函数写入文件。该函数通过替换文件的现有内容来写入数据到文件中。如果没有这样的文件,则创建该文件并写入数据。
语法:
fs.writeFile(path, data, options, callback)
参数:
- path – 要写入的文件的名称或路径。
- data – 要写入文件的内容。
- options – 可选参数,通常我们会指定编码为
utf-8。 - callback – 在文件写入完成后执行。
- error – 如果发生任何错误。
示例 – 创建一个空的文本文件output.txt和app.js文件。
项目结构:

app.js
const fs = require("fs");
const str = "Learning how to write to a file.";
fs.writeFile("output.txt", str, "utf-8", (error) => {
// If there is error then this will execute
if (error) {
console.log(error);
}
else {
console.log("Successfully written!!");
}
// Reading what we have written in file
fs.readFile("output.txt", "utf-8", (error, data) => {
if (error) {
console.log(error);
}
else {
console.log(data);
}
});
});
使用命令运行 app.js 文件 –
node app.js
输出: 将创建一个新文件,文件名为输出.txt ,并在其上写入数据。但如果文件已经存在,则其所有内容将被删除,并被我们写入的数据替代。
Successfully written!!
Learning how to write to a file.
- 在文件中追加: 我们可以使用 fs.appendFile() 将数据追加到文件中。与 fs.writeFile() 不同的是,它不会替换文件的内容,而是将数据添加到文件中。如果找不到文件,则会创建文件并将数据添加到其中。
语法:
fs.appendFile(path, data, options, callback)
参数 –
- path - 文件的名称或路径。
- data - 要添加到文件中的内容。
- options - 通常我们指定编码 – ‘utf-8’
- callback - 在文件追加完毕后执行
- error – 如果有错误
示例 – 创建一个文件 app.js ,并且创建一个空的输出.txt 文件 –
项目结构:

输出.txt: 最初包含 –
Learning how to write to a file. (Previous content)
app.js
const fs = require("fs");
const data = "\nLearning how to append to a file. (New content)";
fs.appendFile("output.txt", data, "utf-8", (error) => {
if (error) {
console.log(error);
}
else {
console.log("Successfully written!!");
}
fs.readFile("output.txt", "utf-8", (error, data) => {
if (error) {
console.log(error);
}
else {
console.log(data);
}
});
});
运行 app.js 文件,使用以下命令 –
node app.js
输出: 我们提供的数据将会追加在输出.txt 中,我们将会得到以下输出 –
Successfully written!!
Learning how to write to a file. (Previous content)
Learning how to append to a file. (New content)
有很多其他的fs模块方法,用于对文件进行各种操作- fs.rename(), fs.unlink(), fs.open(), fs.close()等等。
让我们使用文件系统模块创建一个离线评判项目:
一个离线评判的演示: 假设您有一个JSON文件 test-cases.json ,其中包含一个问题的所有输入测试用例及其输出。
项目结构-

test-cases.json
{
"problem": 121,
"input": [
[5, 3, 4, 2, 1],
[8, 100, 47, 999, 504, 771, 21, 53, 45, 660, 9],
[0, 0, 7, 1, 4, 7, 3, 5],
[1],
[]
],
"output": [3, 673, 19, 1, 10]
}
题目: 找到奇数和偶数之间的差值
// Complete this function to find the
// absolute difference of sum of even
// and odd terms in arr
function diffOfOddEvenSum(arr) {
// Write your code here
}
现在,要创建一个离线评判器,你需要创建一个 app.js 文件并编写以下代码:
app.js
const fs = require("fs");
const funct = require("./index");
function diffOfOddEvenSum(arr) {
let odd = 0;
let even = 0;
arr.forEach((element) => {
if (element % 2 == 1) {
odd += element;
} else even += element;
});
return odd - even;
}
// OFFLINE-JUDGE
// Since this is top level code we can
// use Synchronous version
// reading the data from json file
const jsonData = fs.readFileSync("test.json", "utf-8");
// The data we read from file is in json
// parsing the json to get the data
const testCases = JSON.parse(jsonData);
// Inputs is an array of input cases
const inputs = testCases.input;
// Exptected Output contains the correct
// output of the given problem number
const expectedOutput = testCases.output;
// To store the result of our input test cases
let result = "";
// Calling the diffOdOddEvenSum() function
// to get the output
inputs.forEach((input, index) => {
const output = diffOfOddEvenSum(input);
// console.log(output)
// Checking if the output is correct
if (output == expectedOutput[index]) {
result += `Test Case {index + 1}- passed \n`;
} else {
result += `Test Case{index + 1}- failed \n`;
}
});
// We can use Synchronous version as it
// is top-level code
// writing the output to the file
fs.writeFileSync("result.txt", result, "utf-8");
运行 app.js 文件,可以使用以下命令:
node app.js
输出结果 – 在我们的 result.txt 文件中,我们有
Test Case 1- passed
Test Case 2- passed
Test Case 3- passed
Test Case 4- passed
Test Case 5- failed
通过使用文件系统核心模块,我们可以简单地创建一个离线的评判工具。
极客教程