使用命令行使用 Java 管道输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5918525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Piping Input using Java using command line
提问by mephisto_
public class ReadInput {
public static void main(String[] args) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String x = null;
while( (x = input.readLine()) != null ) {
System.out.println(x);
}
}
}
I am able to run this code from the command line by typing 'java ReadInput < input.txt' but not by typing the input directly like 'java ReadInput hello'. When I type 'java ReadInput hello' I seem to get stuck in an infinite loop for some reason. Shouldn't it just work in the same way as typing 'java ReadInput < input.txt' but instead just reprint 'hello'?
我可以通过键入“java ReadInput < input.txt”而不是像“java ReadInput hello”那样直接键入输入来从命令行运行此代码。当我输入“java ReadInput hello”时,我似乎出于某种原因陷入了无限循环。难道它不应该以与键入“java ReadInput < input.txt”相同的方式工作,而只是重新打印“hello”?
回答by Wyzard
Arguments given on the program's command line don't go to System.in
, they go in the args
array. You could use something like echo hello | java ReadInput
to run the program, or you could modify the program to to look at its args
array and treat it as input. (If you use the latter option, you'd probably want to fall back to reading from System.in
if there's nothing in args
.)
程序命令行上给出的参数不会转到System.in
,它们会出现在args
数组中。您可以使用类似的东西echo hello | java ReadInput
来运行程序,或者您可以修改程序以查看其args
数组并将其视为输入。(如果您使用后一个选项,System.in
如果args
.
回答by RMorrisey
Both of the other two answers above are partially correct, but partially misleading.
上面的另外两个答案都部分正确,但部分具有误导性。
Using the following syntax...
使用以下语法...
java ReadInput < input.txt
...the actual command that is run on the java binary is:
...在 java 二进制文件上运行的实际命令是:
java ReadInput
java ReadInput
The operating system shellinterprets the <
sign, and sends the contents of input.txt to the Standard Input Stream (System.in).
该操作系统壳解释<
符号,和input.txt中的内容发送给标准输入流(System.in)。
When you call System.in.readLine()
, the code checks whether there is a line of input available from the Standard Input Stream. When you piped in a file, this means it takes the next line of the file (as if the OS were taking the contents of the file and typing them in at the prompt). When you run the program without piping a file, it will wait until you, the user, provide it with a line of input from the shell and press the return key.
当您调用 时System.in.readLine()
,代码会检查是否有来自标准输入流的可用输入行。当您输入文件时,这意味着它需要文件的下一行(就像操作系统正在获取文件的内容并在提示符下输入它们一样)。当你在没有管道文件的情况下运行程序时,它会一直等到你,用户,从 shell 向它提供一行输入,然后按回车键。
Command-line arguments to the JVM work differently, and do not use the Standard Input Stream (System.in). Ex:
JVM 的命令行参数的工作方式不同,并且不使用标准输入流 (System.in)。前任:
java ReadInput "Input number one" "Input number two"
In this case, you are passing in two command-line arguments. These properties will be available through the args
array:
在这种情况下,您将传入两个命令行参数。这些属性将通过args
数组提供:
public static void main(String[] args) throws IOException {
System.out.println(args.length); //3
System.out.println(args[0]); //"ReadInput"
System.out.println(args[1]); //"Input number one"
System.out.println(args[2]); //"Input number two"
}
With the code that you provided, the program will end when the result of a readLine() call returns null
. I believeyou may be able to just push enter at the prompt to send a blank line, and end the program. If not, you may have to fix the program to check for an empty string (input.readLine().equals("")).
使用您提供的代码,程序将在 readLine() 调用的结果返回时结束null
。我相信您可以在提示下按 Enter 键发送一个空行,然后结束程序。如果没有,您可能需要修复程序以检查空字符串(input.readLine().equals(""))。
Hope this helps.
希望这可以帮助。
回答by Matt Ball
When you type java ReadInput hello
you have not (yet) provided any input to System.in
so the program execution blocks until there is something to read. You might notice that the string "hello"
is passed into the args
parameter to the main()
method.
当您键入时,java ReadInput hello
您还没有(还)提供任何输入,System.in
因此程序执行会阻塞,直到有内容可以读取。您可能会注意到字符串"hello"
被传递到方法的args
参数中main()
。
If you're using a debugger (which I strongly recommend), place a breakpoint at the first line of main()
. You'll notice that, when you run your program as java ReadInput hello
that args[0]
is "hello"
.
如果您使用调试器(我强烈推荐),请在main()
. 你会发现,当你运行你的程序作为java ReadInput hello
那args[0]
是"hello"
。
If you're not using a debugger, you can use System.out.println()
(this is how beginners frequently debug their code). Add this line of code as the first line of main()
:
如果您不使用调试器,则可以使用System.out.println()
(这是初学者经常调试代码的方式)。添加这行代码作为第一行main()
:
if (args.length > 0) System.out.println(args[0]);
回答by Y.G.
Terminate the standard input with
终止标准输入
Ctrl-D : Linux/Unix
Ctrl-Z (or F6) and "Enter" : Windows.
Or set a special EOF symbol
youself.
或者自己设置一个special EOF symbol
。