Created
November 12, 2015 15:56
-
-
Save ubiquitousthey/7d60913ebd517361e6cd to your computer and use it in GitHub Desktop.
Read Lines from file in Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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