const fs = require('fs');
var readStream=fs.createReadStream('./nodespace/module/1.txt');
var count=0;
var str=''
readStream.on('data',(data)=>{
str+=data;
count++;
})
readStream.on('end',()=>{
console.log(str)
console.log(count)
})
readStream.on('error',(err)=>{
console.log(err)
})
写入
const fs = require('fs');
var str='';
for(var i=0;i<500;i++){
str+='保存数据
';//写入500次
}
var writeStream=fs.createWriteStream('./nodespace/module/1.txt');
writeStream.write(str)
//写入完成
writeStream.end();
writeStream.on('finish',()=>{
console.log("写入完成");
})
管道流(适合大文件)(把一个文件复制到另一个文件夹里)
const fs = require('fs');
var readStream=fs.createReadStream('./1111.jpg')
var writeStream=fs.createWriteStream('./nodespace/module/2222.jpg');
readStream.pipe(writeStream)