Piping Input using Java using command line

感情迁移 提交于 2019-11-30 15:36:09

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.)

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.

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".

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():

if (args.length > 0) System.out.println(args[0]);

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 ReadInput

The operating system shell interprets the < sign, and sends the contents of input.txt to the Standard Input Stream (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.

Command-line arguments to the JVM work differently, and do not use the Standard Input Stream (System.in). Ex:

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:

   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 believe you 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("")).

Hope this helps.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!