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

前端 未结 29 1390
深忆病人
深忆病人 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条回答
  •  温柔的废话
    2020-11-22 04:56

    Since posting my original answer, I found that split is a very easy to use node module for line reading in a file; Which also accepts optional parameters.

    var split = require('split');
    fs.createReadStream(file)
        .pipe(split())
        .on('data', function (line) {
          //each chunk now is a seperate line! 
        });
    

    Haven't tested on very large files. Let us know if you do.

提交回复
热议问题