ReadFileLine
Read file line by line for node
Install
$ npm install readfileline -g
How to use
var fl = require('readfileline');
var fileContents = [];
fl(__dirname+'/data.txt',function(lineData,lineNum){
fileContents.push(lineData);
console.log(lineNum+':'+lineData+'\n');
},function(err,eventType,totalLineNum){
if(eventType=='end'){
console.log('read line is done, total '+totalLineNum+' line(s)');
}
if(eventType=='close'){
console.log('read line is closed, read to '+totalLineNum+' line(s)');
}
if(eventType=='error'){
console.log('read file line has an error:'+err.message);
}
});
var fileContents2=[];
fl(__dirname+'/data.txt',function(lineData,lineNum){
var thisFL = this;
if(lineNum>=5){
this.close();
return;
}
fileContents2.push(lineData);
console.log(lineNum+':'+lineData+'\n');
},function(err,eventType,totalLineNum){
if(eventType=='end'){
console.log('read line is done, total '+totalLineNum+' line(s)');
}
if(eventType=='close'){
console.log('read line is closed, read to '+totalLineNum+' line(s)');
}
if(eventType=='error'){
console.log('read file line has an error:'+err.message);
}
});