nodejs模块之fs&&stream
nodejs的文件系统fs模块,可以实现对文件、目录等的操作,stream模块实现对文件数据的断续读写操作
一、文件操作
fs.readFile(file[,options],callback) 异步读取数据,参数err,data
fs.readFileSync(file[,options]) 同步读取数据,返回读取的数据
fs.writeFile(file, data[,options],callback) 异步写数据,参数err
fs.writeFileSync(file,data[,options]) 同步写数据,返回undefined
fs.stat(path,callback) 返回一个stats对象到回调函数参数中, 通过该stats对象获取文件信息,参数err,stats
fs.statSync(path) 同步,返回一个stats对象
------------------------------------------------------------------
stats对象获取信息方法:
stats.isFile() 是否为文件
stats.isDirectory() 是否为目录(文件夹)
stats.isBlockDevice()
stats.isCharacterDevice()
stats.isSymbolicLink() (only valid with fs.lstat())
stats.isFIFO()
stats.isSocket()
-------------------------------------------------------------
在index.js同级目录下创建一个file.txt, 里面填写数据作为测试用,比如Hello World
1、fs.readFile
"use srict"; const fs = require('fs'); fs.readFile('./file.txt', 'utf-8', function (err, data) {
if (err) {
console.log(err);
return;
}
console.log(data);
});
2、fs.readFileSync
"use srict"; const fs = require('fs'); const data = fs.readFileSync('./file.txt', 'utf-8');
console.log(data);
3、fs.writeFile
读取file.txt文件数据,然后写到data.txt文件中,如果data.txt不存在,会自动创建然后写入,如果存在会先删除data.txt中数据,然后写入覆盖。
"use srict"; const fs = require('fs'); fs.readFile('./file.txt', 'utf-8', function (err, data) {
if (err) {
console.log(err);
return;
}
fs.writeFile('./data.txt', data + ' from file.txt', 'utf-8', function (err) {
if (err) {
console.log(err);
return;
}
console.log('write success');
})
});
4、fs.stat
"use srict"; const fs = require('fs'); fs.stat('./file.txt', (err, stats) => {
if (err) {
console.log(err);
return;
}else {
if (stats.isFile()) {
fs.readFile(fileName, 'utf-8', (err, data) => {
if (err) {
console.log(err);
return;
}
fs.writeFile('./swFile.txt', data + 'from file.txt by stat', 'utf-8', (err) => {
if (err) {
console.log(err);
return;
}
console.log('write success stat');
})
});
} else {
console.log('The file is dirtory');
}
}
});
二、文件夹操作
fs.mkdir(path[,mode],callback) 创建文件夹,参数mode指的是文件夹权限,参数err
fs.mkdirSync(path[,mode]) 同步创建文件夹,返回undefined
fs.readdir(path[,options],callback) 读取文件夹,返回一个数组到回调参数,包含该文件夹所有子内容的名字string,不包括子孙内容,参数err, dir
fs.readdirSync(path[,options]) 同步读取文件夹,返回读取的文件夹数组数据
"use srict"; const fs = require('fs'); fs.readdir('./fsDemo', function (err, dir) {
if (err) {
console.log(err);
return;
}
console.log(dir); // ['demo01', 'demo02']
});
遍历文件夹中所有子孙,然后打印出其名字:
"use strict";
// 遍历目录中所有文件
const fs = require('fs'); bfs('./dir'); function bfs(dirPath) {
const state = fs.statSync(dirPath);
if (state.isFile()) {
console.log(dirPath);
return;
}
if (!state.isDirectory()) {
return;
}
fs.readdir(dirPath, function (err, dirs) {
if (err) {
console.log(err);
return;
}
dirs.forEach((dir, index, arr) => {
bfs(dirPath + '/' + dir);
});
});
}
四、流操作(stream模块)
fs.createReadStream(path[,options]) 创建一个读流操作对象,返回一个readable对象
-------------------------------------------------------
readable对象支持的事件:
data:当有数据读取的时候,返回读取的数据到回调函数参数中
readable:当有数据可读取的时候触发,但不会读取数据,仅仅做判断用
end:当数据全部读取完毕的时候触发
readable对象支持的方法:
rs.pause() 暂停读取数据
rs.resume() 打开读取流,恢复读取数据
rs.read() 读取当前剩余数据并返回,没有则返回null
options 是一个对象或者字符串,各个默认值为
{
flags: 'r',
encoding: null,
fd: null,
mode: 0o666,
autoClose: true
}
---------------------------------------------------
fs.createWriteStream(path[,options]) 写流操作,返回一个writable对象
---------------------------------------------------
writable支持的事件:
finish:当end方法被调用,且所有数据都写入完毕时触发
pipe:当对象通过pipe写入数据时
writable对象支持的方法:
ws.write(chunk[,encoding][,callback]) 写数据,chunk是要写入的数据(string|buffer) ,callback:写入后回调函数
ws.end() 关闭写入流
----------------------------------------------------
pipe方法:
rs.pipe(ws) 从左向右看,创建一个管道,将rs读取的数据通过管道流入到ws写入
-----------------------------------------------------------------------------------------------------------
fs.createReadStream
"use srict"; const fs = require('fs'); const rs = fs.createReadStream('./file.txt', 'utf-8');
// 当读取数据时触发,将读取的数据打印
rs.on('data', function (data) {
console.log(data);
});
// 当有数据可读是,将剩余数据全部打印出来
rs.on('readable', function() {
console.log(rs.read())
});
// 当读取结束时,打印read finished
rs.on('end', function () {
console.log('read finished');
});
fs.createWriteStream
"use strict"; const fs = require('fs'); const rs = fs.createReadStream('./file.txt', 'utf-8');
const ws = fs.createWriteStream('./data.txt', {
flags: 'w', // a 追加,w 删除后再写,默认为w });
// 当读取数据时触发,将读取的数据写入
rs.on('data', function (data) {
ws.write(data);
});
// 当读取数据结束时,关闭写入流,打印write end
rs.on('end', function (data) {
ws.end();
console.log('write end');
}); // 其实可以通过pipe方法,一句代码实现上边的功能,如下
// rs.pipe(ws);
五、文件检测:
fs.watch(filename[,options][,listener]) 监听文件是否改变
------------------------------------------------------------------------------------------
options = {
persistent: 是否持续的检测文件,默认为true,
recursive: 如果检测的是一个目录,是否检测整个目录,默认为false,
encoding: 编码,默认utf-8
}
listener监听函数有两个参数, eventType和filename,
eventType是事件类型'rename' / 'change',
filename是监听的文件名字
rename发生后,无法继续检测
--------------------------------------------------------------------------------------------
使用postcss和autoprefixer这两个插件来检测css文件,如果我们更改了该css内容,则自动读取该css文件内容,然后经过这两个插件处理为响应属性自动添加前缀,然后写入到dest.css 目标文件中,省去了我们增加前缀的时间。
首先我们建立postcss文件,然后建立一个index.css文件和index.js文件,最后在该目录下执行npm init创建一个包,然后执行npm install postcss autoprefixer --save安装这两个插件。
我们在index.js文件中添加如下代码:
"use strict";
// npm init
// cnpm install postcss autoprefixer --save
const postcss = require('postcss');
const fs = require('fs');
const source = "./index.css";
const dest = './dest.css';
// 检测文件是否发生改变,如果发生改变,则读取文件,然后处理,最后写到dest.css目标文件上
fs.watch(source, function () {
fs.readFile(source, 'utf-8', function (err, css) {
postcss([require('autoprefixer')])
.process(css)
.then(function (result) {
// result.css
fs.writeFile(dest, result.css);
console.log('compile success');
});
});
}); console.log('start watch……');
执行node index.js,
然后在index.css代码中添加
div
{
transition: width 2s;
transition-property: width;
transition-duration: 1s;
transition-timing-function: linear;
transition-delay: 2s;
}
最后我们可以看到,该目录下生成了一个dest.css文件,其中就是自动为index.css代码添加前缀后的代码,如下:
div
{
-webkit-transition: width 2s;
transition: width 2s;
-webkit-transition-property: width;
transition-property: width;
-webkit-transition-duration: 1s;
transition-duration: 1s;
-webkit-transition-timing-function: linear;
transition-timing-function: linear;
-webkit-transition-delay: 2s;
transition-delay: 2s;
}
-------------------------------------------------------------------------------------end
nodejs模块之fs&&stream的更多相关文章
- Nodejs模块:fs
/** * @description fs模块常用api */ // fs所有的文件操作都是异步IO,如果要以同步的方式去调用,都会加一个在原同步api的基础上加Sync // 同步的方式会在最后传入 ...
- nodejs中的fs模块中的方法
nodejs中的fs模块 引入模块 const fs =require("fs") 检测文件是否存在fs.stat(path,callback) fs.stat("./n ...
- nodejs学习笔记一( sublime、atom开发环境,http模块,fs模块的初识)
http服务 let server = http.createServer(function(req,res){ }); 监听: server.listen(8080); re ...
- nodejs nodejs模块使用及简单的示例
nodejs模块使用及简单的示例 参考菜鸟教程网:http://www.runoob.com/ 一.fs模块的使用: 1.文件操作: 读文件: //读文件 var fs=require('fs'); ...
- 使用NodeJS模块-NodeJS官方提供的核心模块
除了使用自己写的本地模块以外,NodeJS可以使用另外两种类型的模块,分别是NodeJS官方提供的核心模块和第三方提供的模块 NodeJS官方提供的核心模块 NodeJS平台自带的一套基本的功能模块, ...
- Node.js学习——基本模块之fs
基本模块之fs 异步读文件 异步读取一个文本文件的代码如下: 'use strict'; var fs = require('fs'); fs.readFile('sample.txt', 'utf- ...
- NodeJS 模块开发及发布详解
NodeJS 是一门年轻的语言,扩展模块并不太全,经常我们想用某个模块但是却找不到合适的.比如前两天我需要使用hmac和sha1来做签名,就没有找到一个比较好用的模块,这时候就需要我们自己来实现相应的 ...
- nodejs模块——http模块
http模块主要用于创建http server服务. 本文还用到url模块和path模块,还有fs模块.url模块用于解析url,path模块用于处理和转换文件路径. 一.简单应用 代码如下: // ...
- NodeJS模块、包、NPM
1.NodeJS模块 每一个Nodejs都是一个NodeJS模块,包括JS文件,JSON文本文件,二进制模块文件. a.模块的应用 新建一个文件mytest. ...
随机推荐
- H265 Rtp封包
H265 Rtp封包可以参考Ffmpeg,具体实现在文件rtpenc_h264_hevc.c(4.0.1版本),核心的方法是nal_send 这个方法有些绕,下面帖子具体的代码及注释. static ...
- MQTT--入门
一.简述 MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议),是一种基于发布/订阅(publish/subscribe)模式的“轻量级”通讯协议 ...
- leetCode 15. 3Sum (3数之和) 解题思路和方法
3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...
- 在Java中怎样高效的推断数组中是否包括某个元素
来自 http://www.hollischuang.com/archives/1269? 怎样检查一个数组(无序)是否包括一个特定的值?这是一个在Java中经经常使用到的并且非常实用的操作.同一时候 ...
- php匿名函数和闭包函数及use关键字传参及Closure匿名函数类
php闭包函数用use传参有什么意义?答:use引用外层变量,比如全局变量 Closure,匿名函数,是php5.3的时候引入的,又称为Anonymous functions.字面意思也就是没有定义名 ...
- oracle中把函数的执行权限赋个某个用户
赋权:grant execute on function1 to ucr_dtb1;收回执行权限:revoke execute on function1 from ucr_dtb1; 在ucr_dtb ...
- [转]Ubuntu Server命令行更换软件源
sucd /etc/aptwget http://mirrors.163.com/.help/sources.list.lucidmv sources.list sources.list.backup ...
- Linux进程的虚拟内存区域划分
Linux进程的虚拟内存区域分为:代码区.只读常量区.全局区.BSS段.堆区.栈区 代码区:存储功能代码,函数名所在的区域 只读常量区:存放字符串常量,以及const修饰的全局变量 全局区/数据区:存 ...
- COM线程模型 套间概念
COM线程模型 套间概念 1) 单线程套间.线程是COM主线程,初始化COM库的进程的第一个线程.即使从其他线程访问COM组件,也不需要手工同步,COM库已经实现了同步.寓所线程里有个消息处理循环来处 ...
- 第8章 Foundation Kit介绍
本文转载至 http://blog.csdn.net/mouyong/article/details/16947321 Objective-C是一门非常精巧实用的语言,目前我们还没有研究完它提供的全 ...