function calcLineCount(filename, callback) {
    var fs = require('fs');

    var fileStream = fs.createReadStream(filename),
        lines = 0;

    fileStream.on('data', function (buf) {
        lines += buf.toString().match(/\n/g).length + 1;
    });

    fileStream.on('end', function () {
        callback(lines);
    });
}

calcLineCount('/Users/piaoger/Downloads/lc.js', function(lines) {
    console.log(lines);
});

其实也可以直接在Node.js中调用“wc”来解决问题的。只不过我之前用str.split(/\n/).length来做,应该会比match慢吧。

 

Piaoger

相关文章:

  • 2021-08-26
  • 2021-12-19
  • 2021-09-05
  • 2022-12-23
  • 2021-12-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-21
  • 2021-05-25
  • 2021-07-16
  • 2022-12-23
  • 2021-11-22
  • 2022-12-23
  • 2021-12-20
相关资源
相似解决方案