let fs = require("fs");
let rs = fs.createReadStream('d:/01.wav',{flags:'r'})
let ws = fs.createWriteStream('d:/01-copy.mp4',{flags:"w"})
rs.on('open',function(){
console.log("读取的文件已打开")
})
rs.on("close",function(){
ws.end()
console.log("读取流结束/")
})
rs.on('data',function(chunk){
console.log("单批数据流入:"+chunk.length)
console.log(chunk)
ws.write(chunk,()=>{console.log("单批输入流入完成")})
})
rs.on('data',function(chunk){
console.log("单批数据流入:"+chunk.length)
console.log(chunk)
ws.write(chunk,()=>{console.log("单批输入流入完成")})
})
执行结果如下:
[Running] node "d:devcoding
ode2代码1文件流
ead.js"
读取的文件已打开
单批数据流入:25
<Buffer 6b 6c 6a 6c 6b 6a 6b 6c 6a 6b 6a 6b 6c 6a 6b 6c 6a 6c 6b 6c 6a 6c 6a 6c 6a>
单批数据流入:25
<Buffer 6b 6c 6a 6c 6b 6a 6b 6c 6a 6b 6a 6b 6c 6a 6b 6c 6a 6c 6b 6c 6a 6c 6a 6c 6a>
单批输入流入完成
单批输入流入完成
读取流结束
[Done] exited with code=0 in 0.216 seconds
问题:
代码中监听了data事件两次,是不是代表 流对象也打开和关闭了两次,如果是的话相应的打开关闭提示也应该有两次,不太理解的这个流操作的机制,为社么打开关闭只有一次呢却能读取流对象内容两次呢?