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

前端 未结 29 1376
深忆病人
深忆病人 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 05:09

    I use below code the read lines after verify that its not a directory and its not included in the list of files need not to be check.

    (function () {
      var fs = require('fs');
      var glob = require('glob-fs')();
      var path = require('path');
      var result = 0;
      var exclude = ['LICENSE',
        path.join('e2e', 'util', 'db-ca', 'someother-file'),
        path.join('src', 'favicon.ico')];
      var files = [];
      files = glob.readdirSync('**');
    
      var allFiles = [];
    
      var patternString = [
        'trade',
        'order',
        'market',
        'securities'
      ];
    
      files.map((file) => {
        try {
          if (!fs.lstatSync(file).isDirectory() && exclude.indexOf(file) === -1) {
            fs.readFileSync(file).toString().split(/\r?\n/).forEach(function(line){
              patternString.map((pattern) => {
                if (line.indexOf(pattern) !== -1) {
                  console.log(file + ' contain `' + pattern + '` in in line "' + line +'";');
                  result = 1;
                }
              });
            });
          }
        } catch (e) {
          console.log('Error:', e.stack);
        }
      });
      process.exit(result);
    
    })();
    

提交回复
热议问题