Check end of file in Nodejs with line by line

I use readlineto read line by line from a file and want to determine 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);
});
+7
source share
2 answers

According to node documentation , use closeevent

rd.on('line', function(line) {
console.log(line);
})
.on('close', function(line) {
 // EOF
});
+5
source

you can listen to the event closeto detect the end of the file

rd.on('close', function() {
   // end of file
})
+3
source

Source: https://habr.com/ru/post/1694374/


All Articles