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

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

下载源码hfpp2012发布于Nodejs2137 次点击播放时长:04:03
4

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");
// })
2 条回复
  • 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);
    })

    这样好一点

  • hfpp2012 #2
    ollieSk8 #10 回复

    帮你格式化一下:

    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);
    })
    

    这样好一点

支付宝付款
微信扫码打赏

加我微信:15014006126

加 QQ 群:697272886(1 群)

加 QQ 群:856141852(2 群)

相似的视频

© Rails365 | 隐私条款 | 服务条款 | 粤ICP备15004902号 | 在线学员:25

Top