Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS, fs.readFileSync string-by-string reading, and operating. How?

Sorry for so dumb question,

How i can in NodeJS read from file string by string some value, for example - url, and do operation with each string eventually?

var contents = fs.readFileSync('test.txt', 'utf8');

and what then?

It's need for browserstack+selenium testing. I want run some links one-by-one, from file and do something with them.

Changed code below: from

console.log(lines[i++])

to

line = (lines[i++])
driver.get(line);
driver.getCurrentUrl()
                .then(function(currentUrl) {
                   console.log(currentUrl);

But it works once.

And

var str=fs.readFileSync('test.txt');
str.split(/\n/).forEach(function(line){})


C:\nodejstest>node test1.js
C:\nodejstest\test1.js:57
str.split(/\n/).forEach(function(line){
    ^

TypeError: str.split is not a function
    at Object.<anonymous> (C:\nodejstest\test1.js:57:5)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:142:18)
    at node.js:939:3

Works! Much thanx!

like image 856
Boris Avatar asked Mar 28 '16 18:03

Boris


People also ask

How does fs readFileSync work?

readFileSync() method is an inbuilt application programming interface of fs module which is used to read the file and return its content. In fs. readFile() method, we can read a file in a non-blocking asynchronous way, but in fs. readFileSync() method, we can read files in a synchronous way, i.e. we are telling node.

How do I read a string in node JS?

To get the contents of a file as a string, we can use the readFileSync() or readFile() functions from the native filesystem ( fs ) module in Node. js. The readFileSync() function is used to synchronously read the contents from a file which further blocks the execution of code in Nodejs.

What is the reason to choose fs readFile () method to read a file in node JS?

Reading file Asynchronously Reading a file asynchronously is the simplest way to read the file using fs. readFile() function. It is non-blocking because it doesn't block the rest of the code while executing. As you know, basic JavaScript functions and this asynchronous function are features of JavaScript.

What is the difference between readFile and readFileSync?

readFileSync() is synchronous and blocks execution until finished. These return their results as return values. readFile() are asynchronous and return immediately while they function in the background. You pass a callback function which gets called when they finish.


1 Answers

Another (simpler) method would be to read the entire file into a buffer, convert it to a string, split the string on your line-terminator to produce an array of lines, and then iterate over the array, as in:

var buf=fs.readFileSync(filepath);

buf.toString().split(/\n/).forEach(function(line){
  // do something here with each line
});
like image 105
Rob Raisch Avatar answered Oct 19 '22 23:10

Rob Raisch