Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – NodeJS,fs.readFileSync逐字符串读取和操作.怎么样?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
抱歉这么愚蠢的问题,

我如何在NodeJS中通过字符串读取文件字符串的某些值,例如 – url,并最终对每个字符串进行操作?

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

什么呢?

这需要browserstack SELEnium测试.
我想从文件中逐个运行一些链接并对它们做一些事情.

更改了以下代码

console.log(lines[i++])

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

但它工作一次.

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

作品!
太多了!

解决方法

@H_772_40@ 另一种(更简单的)方法是将整个文件读入缓冲区,将其转换为字符串,在行终止符上拆分字符串以生成行数组,然后遍历数组,如下所示:

var buf=fs.readFileSync(filepath);

buf.toString().split(/\n/).forEach(function(linE){
  // do something here with each line
});

大佬总结

以上是大佬教程为你收集整理的node.js – NodeJS,fs.readFileSync逐字符串读取和操作.怎么样?全部内容,希望文章能够帮你解决node.js – NodeJS,fs.readFileSync逐字符串读取和操作.怎么样?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。