Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read lines synchronously from file in Node.js

I need to parse a file line by line in the following format with Node.js:

13 13 0 5 4 3 0 1 9 12 6 4 5 4 0 2 11 12 9 10 0 6 7 8 9 11 5 3 

It represents a graph. The first two lines are the number of edges and vertexes, followed by the edges.

I can accomplish the task with something like:

var fs = require('fs'); var readline = require('readline'); var read_stream = fs.createReadStream(filename); var rl = readline.createInterface({     input: read_stream }); var c = 0; var vertexes_number; var edges_number; var edges = []; rl.on('line', function(line){     if (c==0) {         vertexes_number = parseInt(line);     } else if (c==1) {         edges_number = parseInt(line);     } else {         edges.push(line.split(' '));     }     c++; }) .on('end', function(){     rl.close(); }) 

I understand this kind of things might not be what Node.js was thought for, but the cascaded if in the line callback does not really look elegant / readable to me.

Is there a way to read synchronously lines from a stream like in every other programming language?

I'm open to use plugins if there is not a built-in solution.

[EDIT]

Sorry, I should have made clearer that I would like to avoid loading the whole file in memory beforehand

like image 929
Andrea Casaccia Avatar asked Dec 11 '15 11:12

Andrea Casaccia


People also ask

How do I read a file line by line in node JS?

Method 1: Using the Readline Module: Readline is a native module of Node. js, it was developed specifically for reading the content line by line from any readable stream. It can be used to read data from the command line. const readline = require('readline');

How do I read a node JS asynchronous file?

readFileSync() method is an inbuilt application programming interface of fs module which is used to read the file and return its content. In fs. readFile() method, we can read a file in a non-blocking asynchronous way, but in fs. readFileSync() method, we can read files in a synchronous way, i.e. we are telling node.

How do I read the last 10 lines of a node js file?

var fs = require('fs'); var readline = require('readline'); var stream = require('stream'); var instream = fs. createReadStream('your/file'); var outstream = new stream; var rl = readline. createInterface(instream, outstream); rl. on('line', function(line) { // process line here }); rl.

How do I read the first line of a node js file?

In all current versions of Node. js, readline. createInterface can be used as an async iterable, to read a file line by line - or just for the first line.


2 Answers

My usual code part for such simple tasks:

var lines = require('fs').readFileSync(filename, 'utf-8')     .split('\n')     .filter(Boolean); 

lines is an array of strings without empty ones.

like image 136
Alexey Ten Avatar answered Sep 17 '22 13:09

Alexey Ten


This project on github.com does exactly what I needed:

https://github.com/nacholibre/node-readlines

var readlines = require('n-readlines'); var liner = new readlines(filename);  var vertexes_number = parseInt(liner.next().toString('ascii')); var edges_number = parseInt(liner.next().toString('ascii')); var edges = []; var next; while (next = liner.next()) {     edges.push(next.toString('ascii').split(' ')); } 
like image 26
Andrea Casaccia Avatar answered Sep 17 '22 13:09

Andrea Casaccia