关于逐行读取文件的解决方案,可以参考这里:https://dev59.com/3WQo5IYBdhLWcg3wfPbK#16013228
var fs = require('fs'),
readline = require('readline'),
instream = fs.createReadStream('/path/to/file');
var rl = readline.createInterface(
{
input: instream,
terminal: false
});
rl.on('line', function(line)
{
console.log(line);
// if(instream.isEnd()) ...
});
如何检测文件是否已读取到末尾?
我知道可以在ReadStream
上使用事件on('end', () => {/*...*/})
来检测,但我需要一个可以通过if
语句进行检查的解决方案。
line
事件之后被触发。因此,我不能在该处理程序中使用它来判断是否为最后一行。 - superhero