I was wondering if it was at all possible to read the very last line of a text file. And then, read the one before that. I can see all the data in the console
, but I have no idea how to just display one line.
Currently, I am using fs
and byline
, to write and read files, respectively.
Use the readline
core module instead of byline
, and keep track of the current and previous lines when you receive events.
var rl = require('readline').createInterface({
input: require('fs').createReadStream('input.file')
});
var current = "";
var prev = "";
rl.on('line', function (line) {
prev = current;
current = line;
});
rl.on('close', function () {
console.log('Last line:', current);
console.log('Prev line:', prev);
});
Alternatively, just read the whole file into a string and then split it after line breaks.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.