In
this post, I am going to show you how to read a file line by line using
readline module.
Step 1: Get a createInterface
instance by passing file read stream as input.
const
rl = readline.createInterface({
input: fs.createReadStream('sample.txt'),
crlfDelay: Infinity
});
Step 2: Read the stream line
by line by using ‘line’ event.
rl.on('line',
(line) => {
console.log(`Line from file: ${line}`);
});
Find
the below working application.
HelloWorld.js
const readline = require('readline'); const fs = require('fs'); /* Create a createInterface instance by passing file read stream as input*/ const rl = readline.createInterface({ input: fs.createReadStream('sample.txt'), crlfDelay: Infinity }); rl.on('line', (line) => { console.log(`${line}`); });
No comments:
Post a Comment