2

I'm having trouble reading both arguments and stdin from the command line when running a java file. I can read in arguments on their own and stdin on it's own but not together; for example:

java myFile 6 2 < numbers.txt

I can get it to store 6 and 2 in an array but then it just stores "<" and "text.txt" also. I've been unable to find anything online describing a similar problem so not really sure where to begin.

Owen S.
  • 7,665
  • 1
  • 28
  • 44
user2152251
  • 43
  • 1
  • 5
  • 5
    Adding your code might be extremely helpful here – barak manos Jan 17 '14 at 19:18
  • 1
    The `> file.txt` mantra is more Unix style, and I'm not sure that Java would support that (since all of the arguments from `args[]` make it into `main()`). Have you tried writing out to a file in Java instead? – Makoto Jan 17 '14 at 19:18
  • Shouldn't you be using '<' for input redirection? – blackcompe Jan 17 '14 at 19:19
  • @blackcompe: For Unix/Linux, `<` reads information into a file, and `>` reads the output of a running script and writes it to a sufficient output pipe. If you want to write out to a file, use `>`. But, that's a script, not Java. – Makoto Jan 17 '14 at 19:20

1 Answers1

2

Command-line arguments are received in the String[]-typed parameter of the main method. Input redirection is done the same as for any other process invoked at the command line. The bytes can be retrieved by reading from stdin until EOF is reached.

Command: java myClass myArg < myFile

public static void main(String[] args)
{
    System.out.println("Arg 1 = " + args[0] + "\nStdin = ");
    try (InputStreamReader isr = new InputStreamReader(System.in)) {
        int ch;
        while((ch = isr.read()) != -1)
            System.out.print((char)ch);
    }catch(IOException e) {
        e.printStackTrace();
    }
}

For more info:

http://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html http://docs.oracle.com/javase/tutorial/essential/io/cl.html

blackcompe
  • 3,180
  • 16
  • 27
  • Perfect! Could you describe how the code knows the difference between the arguments and the stdin? – user2152251 Jan 17 '14 at 19:42
  • brilliant thanks, so if I were to read a string file (opposed to integer in this instance) into an array I would have to change the relevant ints to Strings and place the array insertion line instead of the print line. – user2152251 Jan 17 '14 at 20:03
  • @user2152251: You're talking to code specific without code in your post. What is a string file? All cmd-line arguments are treated as strings. Post a code example or else I can't help you. Sorry. – blackcompe Jan 17 '14 at 20:09
  • I think I could have been clearer in the original post. I'm simply trying to read the contents of the < text.txt file into a string array, so I initially needed help with actually being able to read the file that came under the < input.txt (which your post solved). I was able to read typical arguments but wasn't sure how to read through stdin. – user2152251 Jan 17 '14 at 20:25
  • @user2152251: If you want to read the lines of the redirected input (i.e. the file), you can wrap the `InputStreamReader` in a `BufferedReader` and store each line from `BufferedReader.readLine` into a String array element. See http://stackoverflow.com/questions/14581205/bufferedreader-readline-waits-for-input-from-console – blackcompe Jan 17 '14 at 20:33
  • I just found that out reading some inputreader documents! Works great thanks. – user2152251 Jan 17 '14 at 20:47