equivalent of python's open('abc.txt').xreadlines in node.js

7 views
Skip to first unread message

junyi sun

unread,
Aug 16, 2010, 5:07:30 AM8/16/10
to nod...@googlegroups.com
Hello, I am a newbie of Node.js, which is a great for javascript server-side programming.

I have a trial question, is there a way to read a file or stdin line by line, in a high performance way.

It should be like open('abc.txt').xreadlines in python and io.lines("abc.txt") in Lua.

Thanks.


Mikeal Rogers

unread,
Aug 16, 2010, 11:12:36 AM8/16/10
to nod...@googlegroups.com
require('fs').readFileSync('abc.txt').toString().split('\n').forEach(function (line) { line; })

-Mikeal


--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.

Ben Noordhuis

unread,
Aug 16, 2010, 11:26:49 AM8/16/10
to nod...@googlegroups.com
On Mon, Aug 16, 2010 at 17:12, Mikeal Rogers <mikeal...@gmail.com> wrote:
> require('fs').readFileSync('abc.txt').toString().split('\n').forEach(function
> (line) { line; })

But only for smallish files because this slurps the entire file into
memory. Node core doesn't have a readlines() or xreadlines()
equivalent, that's fairly high level.

Reply all
Reply to author
Forward
0 new messages