createReadStream读取流
代码语言:javascript复制const fs = require('fs');
var readStream = fs.createReadStream('./data.text')
var count = 0
var str = '';
// 监听正在读取
readStream.on('data', (data) => {
str = data;
count ;
})
// 监听读取结束
readStream.on('end', () => {
console.log(str)
console.log(count)
})
// 监听读取错误
readStream.on('err', (err) => {
console.log(err)
})
createWriteStream写入流
代码语言:javascript复制const fs = require('fs');
var str = ''
for (let i = 0; i < 500; i ) {
str = '写入数据n'
}
var writeStream = fs.createWriteStream('./data.text')
writeStream.write(str)
// 标记写入完成
writeStream.end();
writeStream.on('finish', () => {
console.log('写入完成')
})
管道流
代码语言:javascript复制const fs = require('fs');
var readStream = fs.createReadStream('./data.text')
var writeStream = fs.createWriteStream('./getData.text')
// 管道流 复制文件
readStream.pipe(writeStream)