異步建立刪除目錄html
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"); // })刪除文件
遞歸刪除非空文件夾node
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
命令的輸入webpack
文檔:http://nodejs.cn/api/stream.h...
例如:HTTP請求是輸入流,響應是輸出流.
流的應用:web
//把讀取的文件放入一個流中 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
,分段處理
gulp
在參數里加utf-8
,會發現,把那段字符打印出來.api
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
當作一個流來處理.app
新建一個寫入流.
寫入的時候直接使用方法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'); });