4

I'm executing the "Dragon book" front-end compiler, which expects a file input using

java main.Main < fileInput.txt

My question is: when I run args.length, the returned value is 0. Isn't fileInput.txt considered an argument? How could I catch it on code?

2 Answers2

1

No, supplying standard input via redirection is a feature of the shell. The only way to detect use of this is using OS functions on the standard io file handles/descriptors.

d3dave
  • 1,361
  • 14
  • 23
1

No, you are not passing an argument to the program. Instead you are writing to standard input of the executed program (the content of fileInput.txt can be accessed via System.in).

Just in case, example reading:

Scanner scanner = new Scanner(System.in);
while(scanner.hasNextLine()) { 
    System.out.println(scanner.nextLine());
}

Edit: Found a similar question now, see: Reading in from System.in - Java ;-)

Community
  • 1
  • 1
jso
  • 484
  • 5
  • 13