write to file line by line js code example
Example 1: how to right plain text format file in node js
var fs = require('fs')
var logger = fs.createWriteStream('log.txt', {
flags: 'a'
})
logger.write('some data')
logger.write('more data')
logger.write('and more')
Example 2: 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);
});