Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for End of File in Nodejs with line by line read

I am using readline to read line-by-line from a file and want to detect the end of the file.

var fs = require('fs'),
var readline = require('readline');

var rd = readline.createInterface({
  input: fs.createReadStream('/path/to/file'),
  output: process.stdout,
  console: false
});

rd.on('line', function(line) {
  console.log(line);
});
like image 793
MOFD Avatar asked Mar 02 '18 22:03

MOFD


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 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.


1 Answers

As per node documentation, use close event

rd.on('line', function(line) {
console.log(line);
})
.on('close', function(line) {
 // EOF
});
like image 80
fyasir Avatar answered Oct 02 '22 17:10

fyasir