node 拼接buffer

var fs = require("fs");
var chunks = [];
var size = 0;
// 异步读取
var readerStream = fs.createReadStream('t.txt',{highWaterMark :11});

readerStream.on('data', function(chunk) {
    chunks.push(chunk);
    size += chunk.length;
});

readerStream.on('end',function(){
    var buf = Buffer.concat(chunks,size);
    console.log(buf.toString());
});


//
var fs = require("fs");
let buf = Buffer.allocUnsafe(0);

// 异步读取
var readerStream = fs.createReadStream('t.txt',{highWaterMark :1});

readerStream.on('data', function(chunk) {
    buf = Buffer.concat([buf, chunk], buf.length + chunk.length);
});

readerStream.on('end',function(){
    console.log(buf.toString());
});

 

你可能感兴趣的:(nodejs)