fs.js 读取目录 写入内容 和删除 目录
2021-08-08 09:57
标签:rmdir div on() add each encoding require port async let fs = require(‘fs‘) // 封装 异步读取文件 function fsRead(path){ return new Promise(function(resolve,reject){ fs.readFile(path,{flag:"r",encoding:"utf-8"},function(err,data){ if(err){ reject(err); }else{ resolve(data); } }) }) } // 封装 异步写入文件 function fsWrite(path,content){ return new Promise(function(resolve,reject){ fs.writeFile(path,content,{flag:"a",encoding:"utf-8"},function(err){ if(err){ reject(err) }else{ resolve() } }) }) } module.exports = { fsRead, fsWrite } ------------------------ let fs = require(‘fs‘); let { fsRead, fsWrite } = require(‘./fs‘) const txtPath = "all.txt"; // 读取目录 fs.readdir(‘../03-fs‘,function(err,files){ if(err){ console.log(err); }else{ console.log(files); // 循环 写入 内容 files.forEach(async function(filename,i){ // console.log(filename,i); let content = await fsRead(‘../03-fs/‘ + filename) await fsWrite(txtPath,content) }) } }) --------- fs.rmdir(‘abc‘,function(){ console.log(‘删除目录成功‘); }) fs.js 读取目录 写入内容 和删除 目录标签:rmdir div on() add each encoding require port async 原文地址:https://www.cnblogs.com/eric-share/p/14882820.html