Nodejs: each line in a separate file

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

0
source share
2 answers

You can use the Node built-in readlinemodule .

var fs = require('fs');
var readline = require('readline');
var fileCounter = -1;

var file = "foo.txt";
readline.createInterface({
    input: fs.createReadStream(file),
    terminal: false
}).on('line', function(line) {
   var writable = fs.createWriteStream('data/part'+ fileCounter + '.txt', {flags:'w'});
   writable.write(line);
   fileCounter++
});

Note that this will result in the loss of the last line of the file if there is no new line at the end, so make sure that the last line of data matches the new line.

, , 2, :

: 2 - API , , . .

+1

? ? .

var split = require('split');
var fs = require('fs');
var fileCounter = -1;

var readable = fs.createReadStream(file).pipe(split());
readable.on('data', function (line) {
    fileCounter++;
    var writable = fs.createWriteStream('data/part'+ fileCounter + '.txt', {flags:'w'});
    writable.write(line);
    writable.close();
});

...


EDIT: writable ( pipe()), , on('data') event, " , , , ", :

  • KISS
  • ( . Etc...)

, , . , .

0

All Articles