我的问题是我无法确定何时成功写入文件并且文件已关闭。考虑以下情况:
var fs = require('fs');
var outs = fs.createWriteStream('/tmp/file.txt');
var ins = <some input stream>
...
ins.on('data', function(chunk) { out.write(chunk); });
...
ins.on('end', function() {
outs.end();
outs.destroySoon();
fs.stat('/tmp/file.txt', function(err, info) {
// PROBLEM: Here info.size will not match the actual number of bytes.
// However if I wait for a few seconds and then call fs.stat the file has been flushed.
});
});
那么,我如何强制或以其他方式确保在访问文件之前已正确刷新文件?我需要确保文件存在并且完整,因为我的代码必须生成一个外部进程来读取和处理文件。