【发布时间】:2021-10-23 20:45:26
【问题描述】:
我是 javascript 新手,我想检查一个文件的行是否存在于另一个文件中。我有以下代码:
async getReadLiner(filename) {
const fileStream = fs.createReadStream(filename);
const rl = readline.createInterface({
input: fileStream,
});
return rl;
}
有没有可能做类似的事情
checkLines() {
const file1 = await this.getReadLiner(filename1);
const file2 = await this.getReadLiner(filename2);
for await (const line of file1) {
for await (const file2Line of file2) {
if (file2Line == line) // It's not executing this line at all.
return true;
}
}
return false
}
知道为什么这条线(if (file2Line == line) 没有执行吗?
提前致谢。
【问题讨论】:
-
readLines.indexOf("helo 123") != -1 -
@mplungjan - 仅当您将整个文件作为大数组读取时。
readline模块可以避免这种情况,有利于保持流的概念。
标签: javascript node.js file stream readline