Node: How to wait until a file is completed before

2020-07-06 03:31发布

问题:

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.

回答1:

Do your file's post-processing in the writeable stream's close event:

outs.on('close', function() {
  <<spawn your process>>
});

Also, no need for the destroySoon after the end, they are one and the same.



回答2:

Check out https://github.com/TomFrost/FlushWritable – I don't know why it's not written into the Writable core but this should do the trick.

Also, there's a discussion addressing this on GitHub https://github.com/nodejs/node-v0.x-archive/issues/7348.



标签: file node.js io