Node.js 中的屏幕输出加倍,同时逐行读取文件
Doubled output on screen in Node.js, while reading line by line file
我正在使用以下代码在节点中逐行读取文件:
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_();
});
在第 3 个 if 中不会导致第 3 行加倍:[10 20]、[14 50]、[18 32]
这是因为你正在向终端写入两次
output: process.stdout,
和
console.log(line);
查看文档,您还传递了错误的选项,为了达到预期效果,您可以使用终端:false 而不是控制台:false
https://nodejs.org/api/readline.html#readline_readline_createinterface_options
我正在使用以下代码在节点中逐行读取文件:
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_();
});
在第 3 个 if 中不会导致第 3 行加倍:[10 20]、[14 50]、[18 32]
这是因为你正在向终端写入两次
output: process.stdout,
和
console.log(line);
查看文档,您还传递了错误的选项,为了达到预期效果,您可以使用终端:false 而不是控制台:false
https://nodejs.org/api/readline.html#readline_readline_createinterface_options