fs 模块学习文档 fs 模块是 Node.js 中的内置模块,称之为文件系统,提供了对计算机中的磁盘进行操作的功能。本章节将介绍 fs 模块的六个主要操作:文件写入、文件读取、文件移动与重命名、文件删除、文件夹操作和查看资源状态。 一、文件写入 文件写入是将数据保存到文件中的过程。fs 模块提供了四种方式来实现文件写入: 1. writeFile 异步写入 语法:fs.writeFile(file, data[, options], callback) 参数说明: * file:文件名 * data:待写入的数据 * options:选项设置(可选) * callback:写入回调返回值:undefined 代码示例: ``` const fs = require('fs'); fs.writeFile('./座右铭.txt', '三人行,必有我师焉。', err => { if (err) { console.log(err); return; } console.log('写入成功'); }); ``` 2. writeFileSync 同步写入 语法:fs.writeFileSync(file, data[, options]) 参数与 fs.writeFile 大体一致,只是没有 callback 参数 返回值:undefined 代码示例: ``` try { fs.writeFileSync('./座右铭.txt', '三人行,必有我师焉。'); } catch (e) { console.log(e); } ``` 3. appendFile / appendFileSync 追加写入 appendFile 的作用是在文件尾部追加内容,语法与 writeFile 语法完全相同 语法:fs.appendFile(file, data[, options], callback) fs.appendFileSync(file, data[, options]) 返回值:undefined 代码示例: ``` fs.appendFile('./座右铭.txt', '择其善者而从之,其不善者而改之。', err => { if (err) throw err; console.log('追加成功'); }); fs.appendFileSync('./座右铭.txt', '\r\n温故而知新, 可以为师矣'); ``` 4. createWriteStream 流式写入 语法:fs.createWriteStream(path[, options]) 参数说明: * path:文件路径 * options:选项配置(可选) 返回值:Object 流式写入方式适用于大文件写入或者频繁写入的场景。 二、文件读取 文件读取顾名思义,就是通过程序从文件中取出其中的数据。fs 模块提供了三种方式来实现文件读取: 1. readFile 异步读取 语法:fs.readFile(path[, options], callback) 参数说明: * path:文件路径 * options:选项设置(可选) * callback:读取回调返回值:undefined 2. readFileSync 同步读取 语法:fs.readFileSync(path[, options]) 参数与 fs.readFile 大体一致,只是没有 callback 参数 返回值:undefined 3. createReadStream 流式读取 语法:fs.createReadStream(path[, options]) 参数说明: * path:文件路径 * options:选项配置(可选) 返回值:Object 流式读取方式适用于大文件读取或者频繁读取的场景。 三、文件移动与重命名 fs 模块提供了 rename 方法来实现文件移动与重命名。 语法:fs.rename(oldPath, newPath, callback) 参数说明: * oldPath:原文件路径 * newPath:新文件路径 * callback:.rename 回调返回值:undefined 四、文件删除 fs 模块提供了 unlink 方法来实现文件删除。 语法:fs.unlink(path, callback) 参数说明: * path:文件路径 * callback:unlink 回调返回值:undefined 五、文件夹操作 fs 模块提供了 mkdir 和 rmdir 方法来实现文件夹操作。 语法:fs.mkdir(path[, options], callback) 语法:fs.rmdir(path, callback) 六、查看资源状态 fs 模块提供了 stat 和 lstat 方法来实现查看资源状态。 语法:fs.stat(path, callback) 语法:fs.lstat(path, callback) 本节内容只是对 fs 模块的基本介绍,fs 模块还提供了许多其他的方法和选项,可以根据实际需求来选择使用。
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![js](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/release/download_crawler_static/88306956/bg1.jpg)
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/8dc59d5d027049529eb3f61de375d601_weixin_73104281.jpg!1)
- 粉丝: 355
- 资源: 4
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)