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. SQL Agent Job 报“Access to the remote server is denied because the current security context is not trusted”

    SQL Server 2005(Microsoft SQL Server 2005 - 9.00.5000.00)下的一个作业执行一个存储过程,存储过程中动态SQL语句使用链接服务器(Linked S ...

  2. myeclipse10 blue版激活码

    http://blog.itpub.net/27042095/viewspace-1164998/

  3. jvisualVM的使用

    jvisualvm能干什么:监控内存泄露,跟踪垃圾回收,执行时内存.cpu分析,线程分析... jvisualvm已经被集成在jdk1.6以上的版本中(不是jre).自身运行需要最低jdk1.6版本, ...

  4. margin-left:-20px

    对于负的margin值,设置其作用时要和position:absolute;一起使用

  5. 代理 IP 云打码平台的使用

    代理ip 获取代理ip的网站: 快代理 西祠代理 www.goubanjia.com #代理ip import requests headers = { 'User-Agent':'Mozilla/5 ...

  6. 十大PHP程序员必备工具

    十大PHP程序员必备工具 1.Notepad++ 总结来说就是小而精,7.4版本的软件包只有2.9M,比一般的IDE小数十倍,但是Notepad++的功能确是很全面的,代码高亮,语法折叠,宏功能,内置 ...

  7. SQL FULL JOIN 关键字

    SQL FULL JOIN 关键字 只要其中某个表存在匹配,FULL JOIN 关键字就会返回行. FULL JOIN 关键字语法 SELECT column_name(s) FROM table_n ...

  8. 7.01-beautiful_soup

    # pip install beautifulsoup4 from bs4 import BeautifulSoup html_doc = """ <html> ...

  9. 发现电脑上装着liteide,就用golang做一个TCP通讯测试(支持先启动client端和断线重连)

    1.参考https://www.cnblogs.com/yin5th/p/9274495.html server端 main.go package main import ( "fmt&qu ...

  10. 鼠标右键打开命令行cmd(管理员身份)

    参考:https://blog.csdn.net/bdss58/article/details/54745380 添加到注册表 将下面命令保存为reg文件: Windows Registry Edit ...