在打开流之前需要检查流是否已经打开
var fs=require('fs');
var stream=fs.createWriteStream(filePath, {flags: 'a'});
stream.on('open',function(){
});
流
在打开时会发出open
,因为这是异步的,所以无法知道它什么时候会发出,你甚至可以写一些类似的东西
var _path = "text.txt";
var stream1 = fs.createWriteStream(_path,{flags:"a"});
var stream2 = fs.createWriteStream(_path,{flags:"a"});
var stream3 = fs.createWriteStream(_path,{flags:"a"});
var streamN = fs.createWriteStream(_path,{flags:"a"});
当准备就绪时,所有人都会发出open
事件。
如果在发出 Open 时转储console.log
其中一个对象,您将看到一个具有许多属性的对象,例如保留写入状态的_writableState
。
一种不太棒的方法是尝试如下所示的内容
var _path = "text.txt";
var stream = fs.createWriteStream(_path,{flags:"a"});
var stream2 = fs.createWriteStream(_path,{flags:"a"});
var lock = false; // try switching this , when opened and on other events to get the flow.