使用逐行读取的方式,在Node.js中检查文件结尾

20

我正在使用readline逐行读取文件,并希望检测文件的结尾。

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);
});
2个回答

21

根据Node文档,使用close事件。

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

5
你可以监听close事件来检测文件结束。
rd.on('close', function() {
   // end of file
})

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接