'use strict';
const fs = require('fs');
const writable = fs.createWriteStream('file.txt');
const event = fs.createReadStream('./a.log',{highWaterMark:8});
event.pipe(writable);
// 允许多个on('data')监听器
event.on('data',(chunk) =>{
// console.log(123);
console.log(chunk.toString());
});
event.on('end',()=>{
console.log('end');
});
a.log:
aaaaaa1
aaaaaa2
aaaaaa3
aaaaaa4
aaaaaa5
aaaaaa6
控制台以及file.txt都能将完整a.log的内容获取到,说明可读流使用pipe方法内部也是使用的event.on('data'),写到Writable Stream?
'use strict';
const fs = require('fs');
const writable = fs.createWriteStream('file.txt');
const event = fs.createReadStream('./a.log',{highWaterMark:8});
event.pipe(writable);
// 允许多个on('data')监听器
event.on('data',(chunk) =>{
// console.log(123);
console.log(chunk.toString());
});
event.on('end',()=>{
console.log('end');
});
event.on('readable', () => {
console.log('join read()');
let chunk;
// 存在emit,read()返回会为null
while (null !== (chunk = event.read())) {
console.log(`Received ${chunk.length} bytes of data.`);
}
});
实际就是pull与push不能共存,read()可以认为是pasue(pull);on或pipe可以认为是resume(push).详细可以查看这篇文章.或官方api