nodejs fs模块读取文件

var fs = require("fs");

var readStream = fs.createReadStream(path, {'encoding':'utf8'});
var bufferData = [], size = 0;
readStream.on('data', function(chunk){// typeof chunk  "string"
    bufferData.push(new Buffer(chunk));
    size += chunk.length;
});
readStream.on('end', function(){
    var bufferResult = Buffer.concat(bufferData, size);
    console.log(bufferResult.toString());});

 

posted @ 2017-02-28 19:11  sxshijingjing  阅读(877)  评论(0编辑  收藏  举报