Mastering Line Reader A Comprehensive Guide to Efficient File Reading in NodeJS

Introduction to `line-reader` in Node.js

`line-reader` is a powerful Node.js module that allows for efficient reading of files line by line. This can be extremely useful for processing large files without loading the whole file into memory. Whether you are working on log processing, data parsing, or other file manipulation tasks, `line-reader` provides a simple and efficient way to handle files.

Installing `line-reader`

  
    npm install line-reader
  

Basic Usage

  
    const lineReader = require('line-reader');

    lineReader.eachLine('file.txt', function(line, last) {
        console.log(line);
        if (last) {
            console.log('End of file');
        }
    });
  

Reading File Line by Line

  
    const lineReader = require('line-reader');

    lineReader.eachLine('example.txt', (line, last, cb) => {
        console.log('Read line:', line);
        cb();
    });
  

Reading Every Line with Line Number

  
    const lineReader = require('line-reader');

    let lineNumber = 0;
    lineReader.eachLine('sample.txt', (line, last) => {
        console.log(`Line ${++lineNumber}: ${line}`);
    });
  

Handling Errors

  
    const lineReader = require('line-reader');

    lineReader.eachLine('missing.txt', (line) => {
        console.log(line);
    }).catch((err) => {
        console.error('Error:', err);
    });
  

Reading File Synchronously

  
    const lineReader = require('line-reader').sync;

    lineReader.eachLine('file.txt', (line) => {
        console.log(line);
    });
  

Using Callbacks and Promises

  
    const lineReader = require('line-reader');
    
    function readMyFile(filePath) {
        return new Promise((resolve, reject) => {
            let lines = [];
            lineReader.eachLine(filePath, (line, last) => {
                lines.push(line);
                if (last) resolve(lines);
            });
        });
    }

    readMyFile('example.txt').then((lines) => {
        console.log(lines);
    }).catch((err) => {
        console.error(err);
    });
  

Application Example A Simple Log Processor

Here is an example of how to use `line-reader` to process a log file and extract lines that match a specific pattern.

  
    const lineReader = require('line-reader');
    const fs = require('fs');
    
    const logFile = 'server.log';
    const outputFile = 'errors.log';
    const writeStream = fs.createWriteStream(outputFile);
    const errorPattern = /ERROR/;

    lineReader.eachLine(logFile, (line, last) => {
        if (errorPattern.test(line)) {
            writeStream.write(line + '\n');
        }
        if (last) {
            writeStream.end();
            console.log('Log processing completed');
        }
    });
  

In this example, we read lines from `server.log` and check if they contain the word “ERROR”. If they do, we write the line to `errors.log`.

Hash: 10c19e39b47eda1bc06b2b521394051dbcf9975cb6a6cebec1fc5e873039594e

Leave a Reply

Your email address will not be published. Required fields are marked *