如何正确刷新Node.js文件writeStream? [英] How to flush Node.js file writeStream properly?
本文介绍了如何正确刷新Node.js文件writeStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题是我无法确定文件何时成功写入并且文件已关闭。考虑以下情况:
My Problem is that I can not be sure when a file has been successfully written to and the file has been closed. Consider the following case:
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.
});
});
那么,在访问文件之前,如何强制或以其他方式确保文件已正确刷新?我需要确保文件存在且完整,因为我的代码必须生成一个外部进程来读取和处理文件。
So, how do I force or otherwise ensure that the file has been properly flushed before accessing it? I need to be sure that the file is there and complete, as my code has to spawn an external process to read and process the file.
推荐答案
在可写流
关闭
事件:
outs.on('close', function() {
<<spawn your process>>
});
此外,不需要 destroySoon
之后结束
,它们是同一个。
Also, no need for the destroySoon
after the end
, they are one and the same.
这篇关于如何正确刷新Node.js文件writeStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文