首页 > 其他 > 详细

NODE 模块 FS-EXTRA

时间:2019-04-02 17:43:26      阅读:156      评论:0      收藏:0      [点我收藏+]

fs-extra模块是系统fs模块的扩展,提供了更多便利的 API,并继承了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‘)

 

NODE 模块 FS-EXTRA

原文:https://www.cnblogs.com/cangqinglang/p/10643880.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!