我正在使用以下代码在节点中逐行读取文件:const readInterface = readline.createInterface({ input: fs.createReadStream('/path/to/file'), output: process.stdout, console: false});readInterface.on('line', function(line) { console.log(line); if(i == 1) photoNumber = line; if(i == 2) imgFolder = line; if(i == 3) timeString = line; i++;});这是我在终端中得到的输出:3 3 /home/eugen/Pictures/wallpapers /home/eugen/Pictures/wallpapers [10 20], [14 50], [18 32][10 20], [14 50], [18 32]因此,由于某种原因,内容翻了一番。我怎样才能避免这种情况?另外,我观察到某些命令不会使屏幕上的某些内容加倍。例如,使用rl.question("\nDo you want to reset the saved data? (y/n): ", r => { if(r.toLocaleLowerCase() == 'yes' || r.toLocaleLowerCase() == 'y') readData_();});在第三个 if 将导致第三行不加倍:[10 20], [14 50], [18 32]
1 回答
qq_花开花谢_0
TA贡献1835条经验 获得超6个赞
这是因为您要向终端写入两次
output: process.stdout,
和
console.log(line);
查看文档,您还传递了错误的选项,对于您想要的效果,您可以使用终端:假而不是控制台:假
https://nodejs.org/api/readline.html#readline_readline_createinterface_options
添加回答
举报
0/150
提交
取消