I want to split a file: each line in a separate file. The source file is really big. I ended up with the code below:
var fileCounter = -1;
function getWritable() {
fileCounter++;
writable = fs.createWriteStream('data/part'+ fileCounter + '.txt', {flags:'w'});
return writable;
}
var readable = fs.createReadStream(file).pipe(split());
readable.on('data', function (line) {
var flag = getWritable().write(line, function() {
readable.resume();
});
if (!flag) {
readable.pause();
}
});
It works, but it is ugly. Is there an even more vibrant way to do this? possibly with piping and without pause / resume.
NB: this is not a question about lines / files, etc. The question is about threads and I will just try to illustrate it with a problem
source
share