NODE 模塊 FS-EXTRA

fs-extra模塊是系統fs模塊的擴展,提供了更多便利的 API,並繼承了fs模塊的 API。ui

一、複製文件

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

 

示例:this

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

 

二、建立文件、目錄

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

 

示例:spa

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

 

三、移動文件、目錄

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

 

示例:code

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!") })

 

四、寫入文件

outputFile(file, data, callback)

 

示例:blog

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

 

五、刪除文件、目錄

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')
相關文章
相關標籤/搜索