I want to create a write stream and write to it as my data comes in. However, I am able to create the file but nothing is written to it. Eventually, the process runs out of memory.
The problem, I've discovered is that I'm calling write() whilst inside a loop.
Here's a simple example:
'use strict'
var fs = require('fs');
var wstream = fs.createWriteStream('myOutput.txt');
for (var i = 0; i < 10000000000; i++) {
wstream.write(i+'
');
}
console.log('End!')
wstream.end();
Nothing ever gets written, not even hello. But why? How can I write to the file within a loop?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…