node 模块 fs-extra

本文介绍了fs-extra模块的使用方法,包括复制、创建、移动、写入及删除文件和目录等功能。通过具体示例展示了如何利用这些API简化文件操作任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TOC

  • fs模块的扩展,提供了更多便利的 API,并继承了 1、复制文件
    copy(src, dest, [options], callback)

    示例:

    var fs = require('fs-extra'); fs.copy('/tmp/myfile', '/tmp/mynewfile', function(err) { if (err) return console.error(err) console.log("success!") }); fs.copy('/tmp/mydir', '/tmp/mynewdir', function(err) { if (err) return console.error(err) console.log("success!") });

    2、创建文件、目录

    ensureFile(file, callback) createFile(file, callback) createFileSync(file), ensureFileSync(file) ensureDir(dir, callback) ensureDirSync(dir)

    示例:

    var fs = require('fs-extra'); var file = '/tmp/this/path/does/not/exist/file.txt' fs.ensureFile(file, function(err) { console.log(err) // => null  //file has now been created, including the directory it is to be placed in  }); var dir = '/tmp/this/path/does/not/exist' fs.ensureDir(dir, function(err) { console.log(err) // => null  //dir has now been created, including the directory it is to be placed in  });

    3、移动文件、目录

    move(src, dest, [options], callback)

    示例:

    var fs = require('fs-extra') fs.move('/tmp/somefile', '/tmp/does/not/exist/yet/somefile', function(err) { if (err) return console.error(err) console.log("success!") })

    4、写入文件

    outputFile(file, data, callback)

    示例:

    var fs = require('fs-extra') var file = '/tmp/this/path/does/not/exist/file.txt' fs.outputFile(file, 'hello!', function(err) { console.log(err) // => null  fs.readFile(file, 'utf8', function(err, data) { console.log(data) // => hello!  }) })

    5、删除文件、目录

    remove(dir, callback)

    示例:

    var fs = require('fs-extra') fs.remove('/tmp/myfile', function(err) { if (err) return console.error(err) console.log("success!") }) fs.removeSync('/home/jprichardson')

转载于:https://www.cnblogs.com/yanan-boke/p/7772938.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值