腾讯广告投放平台,seosem推广,武安做网站,环境艺术设计网站推荐这里写目录标题 fs模块1) 文件写入2) 文件写入3) 文件移动与重命名4) 文件删除5) 文件夹操作6) 查看资源状态7) 相对路径问题8) __dirname fs模块 
fs模块可以实现与硬盘的交互#xff0c;例如文件的创建、删除、重命名、移动等#xff0c;还有文件内容的写入、读取#xff… 这里写目录标题 fs模块1) 文件写入2) 文件写入3) 文件移动与重命名4) 文件删除5) 文件夹操作6) 查看资源状态7) 相对路径问题8) __dirname  fs模块 
fs模块可以实现与硬盘的交互例如文件的创建、删除、重命名、移动等还有文件内容的写入、读取以及文件夹的相关操作 
1) 文件写入 
文件写入就是将 数据 保存到 文件 中可以使用如下几个方法来实现该效果 
方法说明writeFile异步写入writeFileSync同步写入appendFile / appendFileSync追加写入createWriteStream流式写入 
writeFile 异步写入 
语法 fs.writeFile(file, data[, options], callback) 
参数说明 
file 文件名data 待写入的数据options 选项设置 可选callback 写入回调 
返回值 undefined 
/*** 需要* 新建一个文件座右铭.txt* 写入内容三人行则必有我师焉*/
// 1.导入fs模块
const fs  require(fs)// 2.写入文件
fs.writeFile(./座右铭.txt, 三人行则必有我师焉, err  {// err 写入失败错误对象 写入成功nullif (err) {console.log(写入失败);return;} else {console.log(写入成功);}
});writeFileSync 同步写入 
语法: fs.writeFileSync(file, data[, options]) 
参数与 fs.writeFile 大体一致只是没有 callback 参数 
返回值 undefined 
try{fs.writeFileSync(./座右铭.txt, 三人行必有我师焉。);
}catch(e){console.log(e);
}appendFile / appendFileSync 追加写入 
appendFile 作用是在文件尾部追加内容appendFile 语法与 writeFile 语法完全相同 
语法: 
fs.appendFile(file, data[, options], callback)fs.appendFileSync(file, data[, options]) 
返回值 二者都为 undefined 
// 2.调用appendFile
fs.appendFile(./座右铭.txt, \r\n择其善者而从之。。。。, err{if (err) {console.log(写入失败);return;} else {console.log(写入成功);}
});// 2.调用appendFileSync
fs.appendFileSync(./座右铭.txt, \r\n温故而知新。。。)// 2.调用writeFile(追加或覆盖)
fs.writeFile(./座右铭.txt, love love,{flag:a} ,err{if (err) {console.log(写入失败);return;} else {console.log(写入成功);}
});createWriteStream 流式写入 
语法 fs.createWriteStream(path[, options]) 
参数说明 
path 文件路径options 选项配置 可选  
返回值 Object 
/*** 观书有感.txt*/// 1.导入fs模块
const fs  require(fs);// 2.创建写入流对象
const ws  fs.createWriteStream(./观书有感.txt);// 3.write
ws.write(半亩方塘一鉴开\r\n);
ws.write(天光运功徘徊\r\n);// 4.关闭通道
ws.close();程序打开一个文件是需要消耗资源的 流式写入可以减少打开关闭文件的次数。 流式写入方式适用于 大文件写入或者频繁写入 的场景, writeFile 适合于 写入频率较低的场景 写入文件的场景 
文件写入 在计算机中是一个非常常见的操作下面的场景都用到了文件写入 
下载文件安装软件保存程序日志如 Git编辑器保存文件视频录制 当 需要持久化保存数据 的时候应该想到 文件写入 2) 文件写入 
文件读取顾名思义就是通过程序从文件中取出其中的数据可以使用如下几种方式 
方法说明readFile异步读取readFileSync同步读取createReadStream流式读取 
readFile 异步读取 
语法 fs.readFile(path[, options], callback) 
参数说明 
path 文件路径options 选项配置callback 回调函数 
返回值 undefined 
// 1.引入fs模块
const fs  require(fs)// 2.异步读取
fs.readFile(./观书有感.txt, (err, data)  {if (err) {console.log(读取失败);return;}console.log(data.toString());
});readFileSync 同步读取 
语法 fs.readFileSync(path[, options]) 
参数说明 
path 文件路径options 选项配置 
返回值 string | Buffer 
// 1.引入fs模块
const fs  require(fs)// 2.同步读取
let data  fs.readFileSync(./观书有感.txt);
console.log(data.toString());createReadStream 流式读取 
语法 fs.createReadStream(path[, options]) 
参数说明 
path 文件路径 
返回值 Object 
// 1.引入fs模块
const fs  require(fs);// 2.读取流对象
const rs  fs.createReadStream(./观书有感.txt);// 3.绑定data事件
rs.on(data, chunk  {console.log(chunk);
});// 4.end
rs.on(end, ()  {console.log(读取完成);
});每次取出 64k 数据后执行一次 data 回调 读取完毕后, 执行 end 回调 读取文件应用场景 电脑开机  程序运行  编辑器打开文件  查看图片  播放视频  播放音乐  Git 查看日志  上传文件  查看聊天记录  
3) 文件移动与重命名 
在 Node.js 中我们可以使用 rename 或 renameSync 来移动或重命名 文件或文件夹 
语法 
fs.rename(oldPath, newPath, callback) 
fs.renameSync(oldPath, newPath) 
参数说明 oldPath 文件当前的路径  newPath 文件新的路径  callback 操作后的回调  
// 1.导入fs模块
const fs  require(fs)// 2.调用rename方法
fs.rename(./座右铭.txt, ./我的新座右铭.txt, err  {if (err) {console.log(操作失败);return;}console.log(操作成功);
});4) 文件删除 
在 Node.js 中我们可以使用 unlink 或 unlinkSync 来删除文件 
语法 fs.unlink(path, callback)  fs.unlinkSync(path)  
参数说明 path 文件路径  callback 操作后的回调  
// 1.导入fs模块
const fs  require(fs)// 2.调用 unlink 方法 unlinkSync
fs.unlink(./观书有感.txt, err  {if (err) {console.log(删除失败);return;}console.log(删除成功);
});// 2.调用 rm 方法 rmSync
fs.rm(./data.txt, err  {if (err) {console.log(删除失败);return;}console.log(删除成功);
});5) 文件夹操作 
借助 Node.js 的能力我们可以对文件夹进行 创建 、 读取 、 删除 等操作 
方法说明mkdir / mkdirSync创建文件夹readdir / readdirSync读取文件夹rmdir / rmdirSync删除文件夹 
mkdir 创建文件夹 
在 Node.js 中我们可以使用 mkdir 或 mkdirSync 来创建文件夹 
语法 fs.mkdir(path[, options], callback)  fs.mkdirSync(path[, options])  
参数说明 path 文件夹路径  options 选项配置 可选   callback 操作后的回调  
// 1.导入fs模块
const fs  require(fs)// 2-1.创建文件夹
fs.mkdir(./html, err  {if (err) {console.log(创建失败);return;}console.log(创建成功);
});// 2-2.递归创建
fs.mkdir(./a/b/c, {recursive: true}, err  {if (err) {console.log(创建失败);return;}console.log(创建成功);
});readdir 读取文件夹 
在 Node.js 中我们可以使用 readdir 或 readdirSync 来读取文件夹 
语法 fs.readdir(path[, options], callback)  fs.readdirSync(path[, options])  
参数说明 path 文件夹路径  options 选项配置 可选   callback 操作后的回调  
// 2-3 读取文件夹
fs.readdir(./, (err, data)  {if (err) {console.log(读取失败);return;}console.log(读取成功data);
});rmdir 删除文件夹 
在 Node.js 中我们可以使用 rmdir 或 rmdirSync 来删除文件夹 
语法 fs.rmdir(path[, options], callback)  fs.rmdirSync(path[, options])  
参数说明 path 文件夹路径  options 选项配置 可选   callback 操作后的回调  
// 2-4 删除文件夹
fs.rmdir(./html, err  {if (err) {console.log(删除失败);return;}console.log(删除成功);
});// 递归删除-不推荐使用
fs.rmdir(./a, {recursive: true}, err  {if (err) {console.log(删除失败err);return;}console.log(删除成功);
});// 递归删除-推荐使用
fs.rm(./a, {recursive: true}, err  {if (err) {console.log(删除失败err);return;}console.log(删除成功);
});6) 查看资源状态 
在 Node.js 中我们可以使用 stat 或 statSync 来查看资源的详细信息 
语法 fs.stat(path[, options], callback)  fs.statSync(path[, options])  
参数说明 path 文件夹路径  options 选项配置 可选   callback 操作后的回调  
// 1.导入fs模块
const fs  require(fs);// 2.调用 stat
fs.stat(./1-文件写入.js, (err, data)  {if (err) {console.log(操作失败);return;}console.log(data);console.log(data.isFile());console.log(data.isDirectory());
});7) 相对路径问题 
fs 模块对资源进行操作时路径的写法有两种 相对路径 ./座右铭.txt 当前目录下的座右铭.txt座右铭.txt 等效于上面的写法../座右铭.txt 当前目录的上一级目录中的座右铭.txt  绝对路径 D:/Program Files windows 系统下的绝对路径/usr/bin Linux 系统下的绝对路径  相对路径中所谓的 当前目录 指的是 命令行的工作目录 而并非是文件的所在目录 所以当命令行的工作目录与文件所在目录不一致时会出现一些 BUG 8) __dirname 
__dirname 与 require 类似都是 Node.js 环境中的’全局’变量 
__dirname 保存着 当前文件所在目录的绝对路径 可以使用 __dirname 与文件名拼接成绝对路径 
代码示例 
let data  fs.readFileSync(__dirname  /data.txt);
console.log(data);使用 fs 模块的时候尽量使用 __dirname 将路径转化为绝对路径这样可以避免相对路径产生的Bug 
 文章转载自: http://www.morning.pjjkz.cn.gov.cn.pjjkz.cn http://www.morning.fglxh.cn.gov.cn.fglxh.cn http://www.morning.pnbls.cn.gov.cn.pnbls.cn http://www.morning.wypyl.cn.gov.cn.wypyl.cn http://www.morning.rddlz.cn.gov.cn.rddlz.cn http://www.morning.mxdiy.com.gov.cn.mxdiy.com http://www.morning.bpmtg.cn.gov.cn.bpmtg.cn http://www.morning.bjsites.com.gov.cn.bjsites.com http://www.morning.nqbkb.cn.gov.cn.nqbkb.cn http://www.morning.rnxw.cn.gov.cn.rnxw.cn http://www.morning.tqgmd.cn.gov.cn.tqgmd.cn http://www.morning.pjfmq.cn.gov.cn.pjfmq.cn http://www.morning.mwbqk.cn.gov.cn.mwbqk.cn http://www.morning.xqndf.cn.gov.cn.xqndf.cn http://www.morning.qrqcr.cn.gov.cn.qrqcr.cn http://www.morning.nswcw.cn.gov.cn.nswcw.cn http://www.morning.tfzjl.cn.gov.cn.tfzjl.cn http://www.morning.rrhfy.cn.gov.cn.rrhfy.cn http://www.morning.mpflb.cn.gov.cn.mpflb.cn http://www.morning.qcztm.cn.gov.cn.qcztm.cn http://www.morning.jkrrg.cn.gov.cn.jkrrg.cn http://www.morning.jpdbj.cn.gov.cn.jpdbj.cn http://www.morning.wbhzr.cn.gov.cn.wbhzr.cn http://www.morning.frtb.cn.gov.cn.frtb.cn http://www.morning.sqmbb.cn.gov.cn.sqmbb.cn http://www.morning.pybqq.cn.gov.cn.pybqq.cn http://www.morning.lrplh.cn.gov.cn.lrplh.cn http://www.morning.kdtdh.cn.gov.cn.kdtdh.cn http://www.morning.lrzst.cn.gov.cn.lrzst.cn http://www.morning.xhhqd.cn.gov.cn.xhhqd.cn http://www.morning.lhgqc.cn.gov.cn.lhgqc.cn http://www.morning.reababy.com.gov.cn.reababy.com http://www.morning.gbhsz.cn.gov.cn.gbhsz.cn http://www.morning.zhmgcreativeeducation.cn.gov.cn.zhmgcreativeeducation.cn http://www.morning.kpxzq.cn.gov.cn.kpxzq.cn http://www.morning.rksg.cn.gov.cn.rksg.cn http://www.morning.jpfpc.cn.gov.cn.jpfpc.cn http://www.morning.nlkm.cn.gov.cn.nlkm.cn http://www.morning.crtgd.cn.gov.cn.crtgd.cn http://www.morning.trnl.cn.gov.cn.trnl.cn http://www.morning.cfcdr.cn.gov.cn.cfcdr.cn http://www.morning.bchhr.cn.gov.cn.bchhr.cn http://www.morning.qkxt.cn.gov.cn.qkxt.cn http://www.morning.frpm.cn.gov.cn.frpm.cn http://www.morning.qqpg.cn.gov.cn.qqpg.cn http://www.morning.tralution.cn.gov.cn.tralution.cn http://www.morning.tsdqr.cn.gov.cn.tsdqr.cn http://www.morning.xbckm.cn.gov.cn.xbckm.cn http://www.morning.ggjlm.cn.gov.cn.ggjlm.cn http://www.morning.rnmc.cn.gov.cn.rnmc.cn http://www.morning.htqrh.cn.gov.cn.htqrh.cn http://www.morning.ltkms.cn.gov.cn.ltkms.cn http://www.morning.kqblk.cn.gov.cn.kqblk.cn http://www.morning.tbqbd.cn.gov.cn.tbqbd.cn http://www.morning.scrnt.cn.gov.cn.scrnt.cn http://www.morning.xcyhy.cn.gov.cn.xcyhy.cn http://www.morning.pinngee.com.gov.cn.pinngee.com http://www.morning.pcgrq.cn.gov.cn.pcgrq.cn http://www.morning.xnflx.cn.gov.cn.xnflx.cn http://www.morning.hcszr.cn.gov.cn.hcszr.cn http://www.morning.trfh.cn.gov.cn.trfh.cn http://www.morning.mqbzk.cn.gov.cn.mqbzk.cn http://www.morning.bpmtj.cn.gov.cn.bpmtj.cn http://www.morning.tjmfz.cn.gov.cn.tjmfz.cn http://www.morning.jsljr.cn.gov.cn.jsljr.cn http://www.morning.mqmxg.cn.gov.cn.mqmxg.cn http://www.morning.slmbg.cn.gov.cn.slmbg.cn http://www.morning.jhfkr.cn.gov.cn.jhfkr.cn http://www.morning.tqbyw.cn.gov.cn.tqbyw.cn http://www.morning.nwllb.cn.gov.cn.nwllb.cn http://www.morning.tdfyj.cn.gov.cn.tdfyj.cn http://www.morning.qmqgx.cn.gov.cn.qmqgx.cn http://www.morning.wtwhj.cn.gov.cn.wtwhj.cn http://www.morning.fypgl.cn.gov.cn.fypgl.cn http://www.morning.dwyyf.cn.gov.cn.dwyyf.cn http://www.morning.ndnhf.cn.gov.cn.ndnhf.cn http://www.morning.ebpz.cn.gov.cn.ebpz.cn http://www.morning.rxcqt.cn.gov.cn.rxcqt.cn http://www.morning.bdkhl.cn.gov.cn.bdkhl.cn http://www.morning.ymtbr.cn.gov.cn.ymtbr.cn