Skip to content

Instantly share code, notes, and snippets.

@ubiquitousthey
Created November 12, 2015 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ubiquitousthey/7d60913ebd517361e6cd to your computer and use it in GitHub Desktop.
Save ubiquitousthey/7d60913ebd517361e6cd to your computer and use it in GitHub Desktop.
Read Lines from file in Node.js
var fs = require('fs')
var nameFilename = 'names.txt'
function readLines(filename,fn) {
var stream = fs.createReadStream(filename, {encoding: 'utf8'})
var fileData = ''
stream.on('data', function(data) {
fileData += data;
var lines = fileData.split('\n');
lines.forEach(function(line) {
if(line != '') {
fn(line);
}
});
});
return stream
}
console.time('program');
readLines(nameFilename, function(name) {
console.log(name);
}).on('end', function() {
console.log( "File Read.");
console.timeEnd('program');
},0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment