node.js:使用 readline 从文件中获取输入并在 windows 命令提示符中处理 I/O
node.js: take input from file using readline and process I/O in windows command prompt
我想使用 node.js 从命令提示符 运行 name.js 文件并传递输入文件并将输出重定向到 output.txt,
我正在为此编写一个命令 node name.js < input.txt | > output.txt
但这不起作用或者我错了。
name.js 看起来像这样:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var _line = "";
rl.on('line', function(line){
_line += line;
});
rl.on('pause', function(_line){
console.log(_line);
});
我也在Powershell -Command "command"
中尝试过这个
编辑:
例如 input.txt 包含
hello js
hello node
hello world!
现在,如果我 运行 node name.js < input.txt > output.txt
。
我刚得到 console.log() 的 “undefined” 的 return 值 output.txt
将 _line 传递给 rl.on('pause',function(_line){} 隐藏全局 _line 这就是为什么它给出 undefined 并且 cmd 命令很好。
还有其他方法可以使用 process I/O of node.js
function YourData(input) {
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
YourData(_input);
});
阅读更多关于 readline and process I/O
我想使用 node.js 从命令提示符 运行 name.js 文件并传递输入文件并将输出重定向到 output.txt,
我正在为此编写一个命令 node name.js < input.txt | > output.txt
但这不起作用或者我错了。
name.js 看起来像这样:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var _line = "";
rl.on('line', function(line){
_line += line;
});
rl.on('pause', function(_line){
console.log(_line);
});
我也在Powershell -Command "command"
编辑: 例如 input.txt 包含
hello js
hello node
hello world!
现在,如果我 运行 node name.js < input.txt > output.txt
。
我刚得到 console.log() 的 “undefined” 的 return 值 output.txt
将 _line 传递给 rl.on('pause',function(_line){} 隐藏全局 _line 这就是为什么它给出 undefined 并且 cmd 命令很好。
还有其他方法可以使用 process I/O of node.js
function YourData(input) {
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
YourData(_input);
});
阅读更多关于 readline and process I/O