bjskistad bjskistad - 1 year ago 54
Node.js Question

How to read file with \n character in it?

I read that you can read a file line by line in Node.js like this:

var array = require("fs").readFileSync("file").toString().split("\n");

But, if the file has a
character in it, it splits that too, so you don't get the file lines the right way.

Is there a way I can read a file with
characters and get the files lines correctly?

For example, how would I split this file?

print "\n"

When I run the above code, it results like this:

[ 'file', 'print "\\n"', 'exec' ]


Answer Source

From comment above but it's the same as before ...

var array = require("fs").readFileSync("file").toString().split(String.fromCharCode(10));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download