Reputation: 4116
I currently have an implementation of a program where I read in the input using the Scanner class. Integer by integer. I do this by piping the input file via the command line.
java program < input.txt
I need to avoid piping the input by using an argument on the command line to open the file in my program.
java program --file=input.txt
or something similar. I understand that I could parse the command line argument, extract the text "input.txt" and then use a class like "BufferedReader" or something similar to read the file.
I am just curious if there is away to use the input file (no piping) AND still use the Scanner class. Which means I wouldn't have to change over my nextInt() and other such calls.
Upvotes: 0
Views: 1346
Reputation: 133609
Since arguments are just not evaulated but passed to your main(String[] args)
method without any work in the middle your only option is to parse the argument, extract the filename input.txt
and open it as a normal file stream.
I wouldn't see how it should infer that a file must be opened and passed as a pipe without being a pipe.. you can easily use Scanner
with a File
argument without bothering about anything..
public Scanner(File source) throws FileNotFoundException
Constructs a new Scanner that produces values scanned from the specified file. Bytes from the file are converted into characters using the underlying platform's default charset.
Upvotes: 1