Piping Input using Java using command line

倖福魔咒の 提交于 2019-11-30 15:52:06

问题


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'?


回答1:


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




回答2:


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]);



回答3:


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.




回答4:


Terminate the standard input with

Ctrl-D  :  Linux/Unix 
Ctrl-Z (or F6) and "Enter" : Windows.

Or set a special EOF symbol youself.



来源:https://stackoverflow.com/questions/5918525/piping-input-using-java-using-command-line

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