NodeJS, fs.readFileSync string-by-string reading, and operating. How? NodeJS, fs.readFileSync string-by-string reading, and operating. How? selenium selenium

NodeJS, fs.readFileSync string-by-string reading, and operating. How?


Another (simpler) method would be to read the entire file into a buffer, convert it to a string, split the string on your line-terminator to produce an array of lines, and then iterate over the array, as in:

var buf=fs.readFileSync(filepath);buf.toString().split(/\n/).forEach(function(line){  // do something here with each line});


read file with readable stream and do your operation when you find '\n'.

var fs=require('fs');var readable = fs.createReadStream("data.txt", {    encoding: 'utf8',    fd: null});var lines=[];//this is array not a string!!!readable.on('readable', function() {    var chunk,tmp='';    while (null !== (chunk = readable.read(1))) {        if(chunk==='\n'){            lines.push(tmp);            tmp='';        //    this is how i store each line in lines array        }else            tmp+=chunk;    }});// readable.on('end',function(){//     console.log(lines);    // });readable.on('end',function(){    var i=0,len=lines.length;    //lines is #array not string    while(i<len)        console.log(lines[i++]);});

See if it works for you.