JavaScript逐行读取流

时间:2015-06-09 10:40:28

标签: javascript file csv io

我尝试逐行读取CSV文件并将每行添加到数组中,到目前为止我的代码是:

var identity = WindowsIdentity.GetCurrent();
var groups = from sid in identity.Groups select sid.Translate(typeof(NTAccount)).Value;
foreach (var group in groups)
{
    groupName = group;
}

此日志:

  

var csvFile = 'Domain.csv'; var fs = require('fs'); var readableStream = fs.createReadStream(csvFile); var data = ''; readableStream.on('data', function(chunk) { var temp = ''; temp = chunk; global.qArray.push(temp); }); readableStream.on('end', function() { console.log(global.qArray); });

到控制台。我假设这意味着我的读取流不是逐行接收的。任何人都可以告诉我如何更改它,以便它单独读取每一行?

1 个答案:

答案 0 :(得分:1)

如果你更容易,应该会更容易......

var fs = require('fs'),
readline = require('readline');

 var rd = readline.createInterface({
 input: fs.createReadStream('Domain.csv'),
 output: process.stdout,
 terminal: false
});

rd.on('line', function(line) {
  console.log(line);
});