在 node.js 中一次读取一行文件? [英] Read a file one line at a time in node.js?
问题描述
我正在尝试一次一行读取一个大文件.我发现 Quora 上的一个问题 处理了这个主题,但我缺少一些联系来使整个事情整合在一起.
I am trying to read a large file one line at a time. I found a question on Quora that dealt with the subject but I'm missing some connections to make the whole thing fit together.
var Lazy=require("lazy");
new Lazy(process.stdin)
.lines
.forEach(
function(line) {
console.log(line.toString());
}
);
process.stdin.resume();
我想弄清楚的是如何一次从文件中读取一行,而不是本示例中的 STDIN.
The bit that I'd like to figure out is how I might read one line at a time from a file instead of STDIN as in this sample.
我试过:
fs.open('./VeryBigFile.csv', 'r', '0666', Process);
function Process(err, fd) {
if (err) throw err;
// DO lazy read
}
但它不起作用.我知道在紧要关头我可以退回到使用 PHP 之类的东西,但我想弄清楚这一点.
but it's not working. I know that in a pinch I could fall back to using something like PHP, but I would like to figure this out.
我不认为另一个答案会起作用,因为该文件比我运行它的服务器的内存大得多.
I don't think the other answer would work as the file is much larger than the server I'm running it on has memory for.
推荐答案
从 Node.js v0.12 和 Node.js v4.0.0 开始,有一个稳定的 readline 核心模块.这是从文件中读取行的最简单方法,无需任何外部模块:
Since Node.js v0.12 and as of Node.js v4.0.0, there is a stable readline core module. Here's the easiest way to read lines from a file, without any external modules:
const fs = require('fs');
const readline = require('readline');
async function processLineByLine() {
const fileStream = fs.createReadStream('input.txt');
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
// Note: we use the crlfDelay option to recognize all instances of CR LF
// ('
') in input.txt as a single line break.
for await (const line of rl) {
// Each line in input.txt will be successively available here as `line`.
console.log(`Line from file: ${line}`);
}
}
processLineByLine();
或者:
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream('file.in')
});
lineReader.on('line', function (line) {
console.log('Line from file:', line);
});
最后一行被正确读取(从 Node v0.12 或更高版本开始),即使没有最后的
.
The last line is read correctly (as of Node v0.12 or later), even if there is no final
.
更新:此示例已添加到 Node 的 API 官方文档.
这篇关于在 node.js 中一次读取一行文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!