将 'readline' 用于 node.js 时没有从文本文件中读取任何行
No lines read from text file when using 'readline' for node.js
我在目录中有一个 Node 应用程序以及一个名为 sample.csv.
的文本文件
我正在尝试使用以下代码逐行读取文件,但未读取任何内容:
var readline = require('readline');
var fs = require('fs');
var lineReader = readline.createInterface({
input: fs.createReadStream('sample.csv')
});
lineReader.on('line', function (line) {
console.log(line); // Never happens
});
console.log('Completed.'); // Immediately skips to this
有什么好主意吗? :) 谢谢!
试试这个:
var readline = require('linebyline'),
rl = readline('./sample.csv');
rl.on('line', function (line, lineCount, byteCount) {
console.log(lineCount, line, byteCount);
// do something with the line of text
})
.on('error', function (e) {
console.log("error", e);
// something went wrong
});
节点作弊 可在 line_by_line.
获得
我在目录中有一个 Node 应用程序以及一个名为 sample.csv.
的文本文件我正在尝试使用以下代码逐行读取文件,但未读取任何内容:
var readline = require('readline');
var fs = require('fs');
var lineReader = readline.createInterface({
input: fs.createReadStream('sample.csv')
});
lineReader.on('line', function (line) {
console.log(line); // Never happens
});
console.log('Completed.'); // Immediately skips to this
有什么好主意吗? :) 谢谢!
试试这个:
var readline = require('linebyline'),
rl = readline('./sample.csv');
rl.on('line', function (line, lineCount, byteCount) {
console.log(lineCount, line, byteCount);
// do something with the line of text
})
.on('error', function (e) {
console.log("error", e);
// something went wrong
});
节点作弊 可在 line_by_line.
获得