【问题标题】:How can i read txt file line by line如何逐行读取txt文件
【发布时间】:2016-08-31 16:25:47
【问题描述】:

我想逐行读取txt文件,现在正在显示 我文件中的所有内容,我想将每个新行传递给变量,我该怎么做?

var file = "file:///C:/Users/Viktor/Desktop/test.txt";

function readTextFile(file,line) {

  var rawFile = new XMLHttpRequest();
  rawFile.open("GET", file, true);
  rawFile.onreadystatechange = function () {
    if(rawFile.readyState === 4) {
      if(rawFile.status === 200 || rawFile.status === 0) {
        var allText = rawFile.responseText;

      }
    }
  };
  rawFile.send(null);
}

readTextFile(file);

【问题讨论】:

  • 在新行上拆分

标签: javascript


【解决方案1】:

您可以在新行上拆分字符串。

var allText = rawFile.responseText,
    lines = allText.split(/\n/g);  //.split("\n")
console.log(lines.length);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-09-05
  • 1970-01-01
  • 1970-01-01
  • 2017-11-19
  • 2017-08-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多