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

前端 未结 29 1078
深忆病人
深忆病人 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:04

    With the carrier module:

    var carrier = require('carrier');
    
    process.stdin.resume();
    carrier.carry(process.stdin, function(line) {
        console.log('got one line: ' + line);
    });
    
    0 讨论(0)
  • 2020-11-22 05:08

    In most cases this should be enough:

    const fs = require("fs")
    
    fs.readFile('./file', 'utf-8', (err, file) => {
      const lines = file.split('\n')
    
      for (let line of lines)
        console.log(line)
    });
    
    0 讨论(0)
  • 2020-11-22 05:08

    If you want to read a file line by line and writing this in another:

    var fs = require('fs');
    var readline = require('readline');
    var Stream = require('stream');
    
    function readFileLineByLine(inputFile, outputFile) {
    
       var instream = fs.createReadStream(inputFile);
       var outstream = new Stream();
       outstream.readable = true;
       outstream.writable = true;
    
       var rl = readline.createInterface({
          input: instream,
          output: outstream,
          terminal: false
       });
    
       rl.on('line', function (line) {
            fs.appendFileSync(outputFile, line + '\n');
       });
    };
    
    0 讨论(0)
  • 2020-11-22 05:08

    I have a little module which does this well and is used by quite a few other projects npm readline Note thay in node v10 there is a native readline module so I republished my module as linebyline https://www.npmjs.com/package/linebyline

    if you dont want to use the module the function is very simple:

    var fs = require('fs'),
    EventEmitter = require('events').EventEmitter,
    util = require('util'),
    newlines = [
      13, // \r
      10  // \n
    ];
    var readLine = module.exports = function(file, opts) {
    if (!(this instanceof readLine)) return new readLine(file);
    
    EventEmitter.call(this);
    opts = opts || {};
    var self = this,
      line = [],
      lineCount = 0,
      emit = function(line, count) {
        self.emit('line', new Buffer(line).toString(), count);
      };
      this.input = fs.createReadStream(file);
      this.input.on('open', function(fd) {
        self.emit('open', fd);
      })
      .on('data', function(data) {
       for (var i = 0; i < data.length; i++) {
        if (0 <= newlines.indexOf(data[i])) { // Newline char was found.
          lineCount++;
          if (line.length) emit(line, lineCount);
          line = []; // Empty buffer.
         } else {
          line.push(data[i]); // Buffer new line data.
         }
       }
     }).on('error', function(err) {
       self.emit('error', err);
     }).on('end', function() {
      // Emit last line if anything left over since EOF won't trigger it.
      if (line.length){
         lineCount++;
         emit(line, lineCount);
      }
      self.emit('end');
     }).on('close', function() {
       self.emit('close');
     });
    };
    util.inherits(readLine, EventEmitter);
    
    0 讨论(0)
  • 2020-11-22 05:09
    require('fs').readFileSync('file.txt', 'utf-8').split(/\r?\n/).forEach(function(line){
      console.log(line);
    })
    
    0 讨论(0)
  • 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);
    
    })();
    
    0 讨论(0)
提交回复
热议问题