检查Nodejs中的文件结尾,逐行读取

问题描述 投票:6回答:2

我正在使用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);
});
javascript node.js regex
2个回答
3
投票

根据node documentation,使用close事件

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

3
投票

你可以听close事件来检测文件的结尾

rd.on('close', function() {
   // end of file
})
© www.soinside.com 2019 - 2024. All rights reserved.