Wednesday, August 1, 2018

javascript - Read a file one line at a time in node.js?



I am trying to read a large file one line at a time. I found a question on Quora that dealt with the subject but I'm missing some connections to make the whole thing fit together.



 var Lazy=require("lazy");
new Lazy(process.stdin)

.lines
.forEach(
function(line) {
console.log(line.toString());
}
);
process.stdin.resume();


The bit that I'd like to figure out is how I might read one line at a time from a file instead of STDIN as in this sample.




I tried:



 fs.open('./VeryBigFile.csv', 'r', '0666', Process);

function Process(err, fd) {
if (err) throw err;
// DO lazy read
}



but it's not working. I know that in a pinch I could fall back to using something like PHP, but I would like to figure this out.



I don't think the other answer would work as the file is much larger than the server I'm running it on has memory for.


Answer



Since Node.js v0.12 and as of Node.js v4.0.0, there is a stable readline core module. Here's the easiest way to read lines from a file, without any external modules:



var lineReader = require('readline').createInterface({
input: require('fs').createReadStream('file.in')
});


lineReader.on('line', function (line) {
console.log('Line from file:', line);
});


The last line is read correctly (as of Node v0.12 or later), even if there is no final \n.



UPDATE: this example has been added to Node's API official documentation.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...