剧场模式
首页后端Nodejs轻松学 nodejs - 基础篇

轻松学 Node.js - 基础篇 #7 创建和删除目录

求知小风 · 元婴发布于
8

var fs = require('fs');

fs.mkdir('stuff', function() {
    fs.readFile('readMe.txt', 'utf8', function(err, data) {
        fs.writeFile('./stuff/writeMe.txt', data, function() {
            console.log('copy successfully');
        })
    })
});

// fs.unlink("writeMe.txt", function() {
//     console.log("delete writeMe.txt file");
// })
3 条回复
  • ollieSk8 · 元婴
    ollieSk8 · 元婴 #1

    const fs = require('fs');
    const mkdir = function (dirname) {
    return new Promise(function (resolve, reject) {
    fs.mkdir(dirname,()=>{
    resolve();
    });
    });
    };
    const readFile = function (fileName) {
    return new Promise(function (resolve, reject) {
    fs.readFile(fileName, function(error, data) {
    if (error) return reject(error);
    resolve(data);
    });
    });
    };
    const writeFile = function (fileName,data) {
    return new Promise(function (resolve, reject) {
    fs.writeFile(fileName,data,()=>{
    resolve(1);
    })
    });
    };
    const asyncReadFile = async function () {
    const f1 = await mkdir('stuff');
    const f2 = await readFile('./abc.txt');
    const f3 = await writeFile('./stuff/abc_copy.txt',f2);
    };
    asyncReadFile().then((d)=>{
    console.log('copy success);
    })

    这样好一点

  • 求知小风 · 元婴

    帮你格式化一下:

    const fs = require('fs');
    const mkdir = function(dirname) {
        return new Promise(function(resolve, reject) {
            fs.mkdir(dirname, () = >{
                resolve();
            });
        });
    };
    const readFile = function(fileName) {
        return new Promise(function(resolve, reject) {
            fs.readFile(fileName,
            function(error, data) {
                if (error) return reject(error);
                resolve(data);
            });
        });
    };
    const writeFile = function(fileName, data) {
        return new Promise(function(resolve, reject) {
            fs.writeFile(fileName, data, () = >{
                resolve(1);
            })
        });
    };
    const asyncReadFile = async
    function() {
        const f1 = await mkdir('stuff');
        const f2 = await readFile('./abc.txt');
        const f3 = await writeFile('./stuff/abc_copy.txt', f2);
    };
    asyncReadFile().then((d) = >{
        console.log('copy success);
    })
    

    这样好一点

  • mike652638 · 元婴
    mike652638 · 元婴 #3

    fs.rmdir()删除非空目录(目录下存在文件/子文件夹)时会报错: Failed to delete the folder: Error: ENOTEMPTY: directory not empty ...

    在StackOverFlow(https://stackoverflow.com/questions/12627586/is-node-js-rmdir-recursive-will-it-work-on-non-empty-directories/12761924#12761924)搜到的解决方案:

    1. 递归同步删除
      deleteFolderRecursive = function(path) {
      var files = [];
      if( fs.existsSync(path) ) {
      files = fs.readdirSync(path);
      files.forEach(function(file,index){
      var curPath = path + "/" + file;
      if(fs.lstatSync(curPath).isDirectory()) { // recurse
      deleteFolderRecursive(curPath);
      } else { // delete file
      fs.unlinkSync(curPath);
      }
      });
      fs.rmdirSync(path);
      }
      };

    2. 使用https://www.npmjs.com/package/rimraf

    3.使用https://github.com/jprichardson/node-fs-extra

官方服务号
随机课程
跟我一起学 React + dva + Mockjs

跟我一起学 React + dva + Mockjs

9 个视频34 分钟中级

Pro¥ 119.00¥ 95.20

React已完结

学员(132)
ahatom · 练虚Summer · 元婴mrj · 大罗j921216063 · 道祖叶 · 太乙皮卡丘 · 合体caitengfei520 · 元婴招财加菲猫 · 大罗奥特曼🤖 · 元婴可爱帅 · 太乙Treeman · 元婴Q.Qiang · 元婴
最新动态
ahatom · 练虚学习到了4:35
Summer · 元婴学习到了4:34
mrj · 大罗学习到了4:34
j921216063 · 道祖学习到了4:33
叶 · 太乙学习到了4:37
统计信息
    学员: 17786
    视频数量: 1049
    帖子数量: 427

© 汕尾市求知科技有限公司 | 粤ICP备19038915号 | 关注我们 | 在线学员:116

Top