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的更多相关文章

  1. [Nodejs] node的fs模块

    fs 模块 Node.js 提供一组类似 UNIX(POSIX)标准的文件操作 API. Node 导入文件系统模块(fs).Node.js 文件系统(fs 模块)模块中的方法均有异步和同步版本,例如 ...

  2. Node内核基本自带模块fs 文件的读写

    在node的内核中存在一些内置的模块 这些是最基本的服务端所必要的 1:node全局环境:global类似于浏览器端的window 2:文件读取模块:fs fs模块同时提供了异步和同步的方法. 'us ...

  3. 【node】fs模块,文件和目录的操作

    检查文件是否存在,查询文件信息 fs.stat() fs.stat('./server.js', function (err, stat) { if (stat && stat.isF ...

  4. node的fs模块使用————node

    node的fs模块使用----node fs模块是调用文件的模块. var fs=require('fs'); //引用模块. //查看文件信息 fs.stat('index.txt',functio ...

  5. 从官网学习Node.js FS模块方法速查

    最新文档请查看仓库 https://github.com/wangduandu... 1. File System 所有文件操作提供同步和异步的两种方式,本笔记只记录异步的API 异步方式其最后一个参 ...

  6. Node.js FS模块方法速查

    1. File System 所有文件操作提供同步和异步的两种方式,本笔记只记录异步的API 异步方式其最后一个参数是回调函数.回调函数的第一个参数往往是错误对象,如果没有发生参数,那么第一个参数可能 ...

  7. nodejs模块——fs模块

    fs模块用于对系统文件及目录进行读写操作. 一.同步和异步 使用require('fs')载入fs模块,模块中所有方法都有同步和异步两种形式. 异步方法中回调函数的第一个参数总是留给异常参数(exce ...

  8. Commonjs规范及Node模块实现

    前面的话 Node在实现中并非完全按照CommonJS规范实现,而是对模块规范进行了一定的取舍,同时也增加了少许自身需要的特性.本文将详细介绍NodeJS的模块实现 引入 nodejs是区别于java ...

  9. 模块机制 之commonJs、node模块 、AMD、CMD

    在其他高级语言中,都有模块中这个概念,比如java的类文件,PHP有include何require机制,JS一开始就没有模块这个概念,起初,js通过<script>标签引入代码的方式显得杂 ...

  10. 【转】Commonjs规范及Node模块实现

    前言: Node在实现中并非完全按照CommonJS规范实现,而是对模块规范进行了一定的取舍,同时也增加了少许自身需要的特性.本文将详细介绍NodeJS的模块实现 引入 nodejs是区别于javas ...

随机推荐

  1. MyBatis笔记----(2017年)最新的报错:Cannot find class [org.apache.commons.dbcp.BasicDataSource] for bean with name 'dataSource' defined in class path resource [com/ij34/mybatis/applicationContext.xml]; nested e

    四月 05, 2017 4:56:11 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRef ...

  2. [20190306]奇怪的查询结果.txt

    [20190306]奇怪的查询结果.txt--//链接http://www.itpub.net/thread-2108588-1-1.html提到一个非常古怪的问题,我自己重复测试看看:1.环境:SC ...

  3. MongoDB 主从和Replica Set

    目前主要的MongoDB高可用架构包含: 主从架构 Replica set副本集方式 sharding分片 注意:使用高可用架构后ips,qps相比单实例都会有一定程度的下降,其中rs下降不是他太明显 ...

  4. JSON Web Tokens简单学习

    JWT用于加密生成安全认证的令牌,存储登录验证成功的部分用户信息 一.安装JWT 二.加密 解密 代码 /*存储在加密字符串的信息*/ var payload = new Dictionary< ...

  5. mac os 10.12 Sierra 连接 惠普 M1136 MFP 打印机,通过 samba 协议,安装驱动,连接打印机

    参考链接: https://support.hp.com/hk-zh/product/hp-zbook-17-g3-mobile-workstation/8693765/document/c04530 ...

  6. JS 代码中到底加不加分号

    背景 在写自动执行函数时 vm.$watch('datas', function() { console.log(vm.datas); }) (function () { console.log('t ...

  7. FCM算法的matlab程序(初步)

    FCM算法的matlab程序 在https://www.cnblogs.com/kailugaji/p/9648430.html文章中已经介绍了FCM算法,现在用matlab程序实现它. 作者:凯鲁嘎 ...

  8. Mac各种数据库安装和启动【笔记】

    MongoBD 一个基于分布式文件存储的数据库. 下载 https://www.mongodb.com/download-center#community 安装 解压包 mongodb 数据默认存在/ ...

  9. C#基础知识之Partial class

    C# 2.0 可以将类.结构或接口的定义拆分到两个或多个源文件中,在类声明前添加partial关键字即可. 例如:下面的PartialTest类 class PartialTest { string ...

  10. UEditor学习笔记1

    首先对于前段时间遇到的一些总结做下记录: 0 == ''  => true transition,渐变属性,其值可以是某属性,也可以是all,如transition: all 0.3s:鼠标放到 ...