Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a text file line by line in JavaScript?

I need to read a text file line by line in JavaScript.

I might want to do something with each line (e.g. skip or modify it) and write the line to another file. But the specific actions are out of the scope of this question.

There are many questions with similar wording, but most actually read the whole file into memory in one step instead of reading line by line. So those solutions are unusable for bigger files.

like image 831
Ark-kun Avatar asked Dec 11 '25 23:12

Ark-kun


2 Answers

With Node.js a new function was added in v18.11.0 to read files line by line

  • filehandle.readLines([options])

This is how you use this with a text file you want to read

import { open } from 'node:fs/promises';
myFileReader();
async function myFileReader() {
    const file = await open('./TextFileName.txt');
    for await (const line of file.readLines()) {
        console.log(line)
    }
}

To understand more read Node.js documentation here is the link for file system readlines(): https://nodejs.org/api/fs.html#filehandlereadlinesoptions

like image 83
Larry Avatar answered Dec 13 '25 11:12

Larry


The code to read a text file line by line is indeed surprisingly non-trivial and hard to discover.

This code uses NodeJS' readline module to read and write text file line by line. It can work on big files.

const fs = require("fs");
const readline = require("readline");

const input_path = "input.txt";
const output_path = "output.txt";

const inputStream = fs.createReadStream(input_path);
const outputStream = fs.createWriteStream(output_path, { encoding: "utf8" });
var lineReader = readline.createInterface({
  input: inputStream,
  terminal: false,
});
lineReader.on("line", function (line) {
  outputStream.write(line + "\n");
});

like image 44
Ark-kun Avatar answered Dec 13 '25 11:12

Ark-kun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!