Read a file one line at a time in node.js?

前端 未结 29 1115
深忆病人
深忆病人 2020-11-22 04:33

I am trying to read a large file one line at a time. I found a question on Quora that dealt with the subject but I\'m missing some connections to make the whole thing fit to

29条回答
  •  -上瘾入骨i
    2020-11-22 04:55

    Generator based line reader: https://github.com/neurosnap/gen-readlines

    var fs = require('fs');
    var readlines = require('gen-readlines');
    
    fs.open('./file.txt', 'r', function(err, fd) {
      if (err) throw err;
      fs.fstat(fd, function(err, stats) {
        if (err) throw err;
    
        for (var line of readlines(fd, stats.size)) {
          console.log(line.toString());
        }
    
      });
    });
    

提交回复
热议问题