异步创建删除目录
var fs = require('fs');
fs.mkdir('stuff', function() {
fs.readFile('readMe.txt', 'utf8', function(err, data) {
fs.writeFile('./stuff/writeMe.txt', data, function() {
console.log('copy successfully');
})
})
});
// fs.unlink("writeMe.txt", function() {
// console.log("delete writeMe.txt file");
// })删除文件
递归删除非空文件夹
function delDir(path){
let files = [];
if(fs.existsSync(path)){
files = fs.readdirSync(path);
files.forEach((file, index) => {
let curPath = path + "/" + file;
if(fs.statSync(curPath).isDirectory()){
delDir(curPath); //递归删除文件夹
} else {
fs.unlinkSync(curPath); //删除文件
}
});
fs.rmdirSync(path);
}
}
module.exports = delDir;
举例:在命令行中
ls
命令:查看当前目录下的文件和目录
grep
:筛选,例如是否包含"app"这个关键字
ls
输出一个流(输出一个信息),这个流作为了grep
命令的输入
文档:http://nodejs.cn/api/stream.h... 例如:HTTP请求是输入流,响应是输出流. 流的应用:
//把读取的文件放入一个流中
var fs = require('fs')
var myReadStream = fs.createReadStream(__dirname + '/readMe.txt')
//流是一个事件的实例,所以可以放置监听事件(流可以是可读的、可写的、或者可读可写的。 所有的流都是 EventEmitter 的实例。)
myReadStream.on('data',function(chunk) {//chunk是块的意思
console.log('new chunk received')
console.log(chunk)
})
readMe.txt
文件有5000行的字符,把文件分成一个个buffer
,分段处理
在参数里加utf-8
,会发现,把那段字符打印出来.
on
是在接收数据的时候的监听函数,end
代表在接受数据完成之后的数据
var fs = require('fs')
var myReadStream = fs.createReadStream(__dirname + '/readMe.txt')
myReadStream.setEncoding('utf8')//编码也可以这样写
var data = ''
myReadStream.on('data',function(chunk) {
data+=chunk
})
myReadStream.on('end',function () {
console.log(data)
})
以上就是把一个readMe.txt
当做一个流来处理.
新建一个写入流. 写入的时候直接使用方法write,参数为数据片段 把readMe.txt里面的内容写到writeMe2.txt里面
var fs = require('fs')
var myReadStream = fs.createReadStream(__dirname + '/readMe.txt')
var myWriteStream = fs.createWriteStream(__dirname+'/writeMe2.txt')//新建一个写入流.
myReadStream.setEncoding('utf8')
var data = ''
myReadStream.on('data',function(chunk) {
myWriteStream.write(chunk)//写入的时候直接使用方法write,参数为数据片段
})
myReadStream.on('end',function () {
// console.log(data)
})
或者: 把'hello world'写入''writeMe2.txt' 文件
var myWriteStream = fs.createWriteStream(__dirname+'/writeMe2.txt')
var writeData = 'hello world'
myWriteStream.write(writeData)
myWriteStream.end()
myWriteStream.on('finish',function () {
console.log('finish')
})
实现复制内容的操作
var myReadStream = fs.createReadStream(__dirname + '/readMe.txt')
var myWriteStream = fs.createWriteStream(__dirname+'/writeMe2.txt')
myReadStream.pipe(myWriteStream)
把readMe的内容复制到writeMe2 管道运送流
加密,压缩的过程
// 压缩
var crypto = require('crypto');
var fs = require('fs');
var zlib = require('zlib');
var password = new Buffer(process.env.PASS || 'password');
var encryptStream = crypto.createCipher('aes-256-cbc', password);
var gzip = zlib.createGzip();
var readStream = fs.createReadStream(__dirname + "/readMe.txt"); // current file
var writeStream = fs.createWriteStream(__dirname + '/out.gz');
readStream // reads current file
.pipe(encryptStream) // encrypts
.pipe(gzip) // compresses
.pipe(writeStream) // writes to out file
.on('finish', function() { // all done
console.log('done');
});
// 解压
var crypto = require('crypto');
var fs = require('fs');
var zlib = require('zlib');
var password = new Buffer(process.env.PASS || 'password');
var decryptStream = crypto.createDecipher('aes-256-cbc', password);
var gzip = zlib.createGunzip();
var readStream = fs.createReadStream(__dirname + '/out.gz');
readStream // reads current file
.pipe(gzip) // uncompresses
.pipe(decryptStream) // decrypts
.pipe(process.stdout) // writes to terminal
.on('finish', function() { // finished
console.log('done');
});