测试博客

使用 fs 模块读取与写入文件

这篇文章属于系列 Node.js 实用技巧,位序为 2,有这些 tag:Node.js, 文件系统, fs, IO操作

Node.js 的 fs 模块提供了丰富的文件系统操作 API:

js
const fs = require('fs');

fs.readFile('input.txt', 'utf-8', (err, data) => {
  if (err) throw err;
  console.log(data);
});

常用方法

方法描述
readFile异步读取文件
writeFile异步写入文件
unlink删除文件
  • 支持 Promise 化操作(fs.promises
  • 也可同步操作但不推荐

Node.js 擅长处理 IO 密集型任务。