nodejs read specific line from file code example
Example 1: node.js read text file line by line
const readline = require('readline');
const readInterface = readline.createInterface({
input: fs.createReadStream('name.txt'),
output: process.stdout,
console: false
});
for await (const line of readInterface) {
console.log(line);
}
readInterface.on('line', function(line) {
console.log(line);
});
Example 2: how to get a particular line from a file in nodejs
lineReader.open('/path/to/file', function(reader) {
if (reader.hasNextLine()) {
reader.nextLine(function(line) {
console.log(line);
});
}
});