node.js - line-reader 逐行读取文件 - nodejs
问题描述
我正在尝试使用 line-reader 模块逐行读取一些文件(001.txt 到 654.txt)。当我执行我的代码时,它只适用于几个文件,其余的我只得到一行。例如,我只得到 002.txt 的第一行。附上该文件的图像以供参考,代码如下。
const lineReader = require("line-reader");
const f = path.join(__dirname + file);
lineReaderParam.open(f, { bufferSize: 1024 }, (error, reader) => {
// reader.nextLine((error, line) => {
// console.log("line 233", line);
// songLineSections.title = line;
// });
if (reader.hasNextLine()) {
while (reader.hasNextLine()) {
reader.nextLine((err, line) => {
// if (line === "Choeur") {
// section = { num: "", text: [] };
// section.choeur = line;
// } else
if (line === "Refrain") {
section = { num: "", text: [] };
section.refrain = line;
//songLineSections.push(section);
} else if (line === "") {
//section = { num: "", text: [] };
songLineSections.push(section);
if (section.num === sectionNum) {
section = { num: "", text: [] };
if (songLineSections.length !== 0) {
songsList.push(songLineSections);
}
} else {
if (line === "") {
section = { num: "", text: [] };
}
}
} else {
if (isNaN(line)) {
section.text.push(line);
songsList.push(songLineSections);
} else {
if (sectionNum !== line) {
section.num = line;
sectionNum = line;
}
}
}
});
}
} else {
reader.close(error => {
console.log(error);
});
}
songLineSections.push(section);
console.log("line 80", songLineSections);
event.reply("songNumberReply", songLineSections);
return songLineSections;
});
解决方案
推荐阅读
- java - 将阿拉伯数字转换为罗马数字的问题
- c# - 在 ASP.Net 中单击按钮后,报表查看器始终显示加载但从不加载数据
- spring - 具有嵌套属性的 Spring REST 请求(在树结构中)
- reactjs - 使用 react-bootstrap 时,只能在函数组件的主体内部调用钩子
- string - 用于格式化日期和时间的 Dart 函数
- ssh - mdt shell 未连接或推送 ssh 密钥
- angular - Angular 中的链式 Observable RxJ
- python - 从python中删除sql表行
- javascript - 如何在 $match 聚合 mongodb 上分配动态键?
- c# - Twilio API - 发送短信时请求过多