I am missing something regarding redirection and Stream

Using Scanner, am I able to redirect on the fly? I am familiar with redirection in C. Is redirection in Java similar? Do I have to redirect in the following manner:
java myprogram < mytestfile.txtor can I redirect on the fly with Scanner?
Also, is end of file in Java the same as when I provide Scanner input and hit return?
What I desire to do is allow both console use and redirection. If the user prefers redirection, then my program will use Scanner and StringTokenizer to parse each line of the file, ignoring /n and sending a long concatenated string as input further along. If the user prefers console, then I have a method that will read a file and the program continues.
Edited by: 832844 on Feb 24, 2011 8:29 AM

jverd wrote:
>
If you mean a ? b : c, see [url http://download.oracle.com/javase/tutorial/java/nutsandbolts/op2.html]here and/or google for java ternary operator.
What I would ideally like to do is exactly the following: Allow console input using the keyboard and Scanner or allow redirection into Scanner from a file.That's pretty vague.
This is not a vague question at all. My desire is to allow a user of my program at their leisure to run the program and provide input from the keyboard. The program will obviously, for simplicity sake, use Scanner. Now if the user happens to have multiple input they do not want to type into the console using their keyboard, they can feel free to have the option of redirecting input from a file.
Now this is all to be done using:
Scanner keyboard = new Scanner(System.in)That is it, there are no multiple instances of Scanner. This is the front door of the program and it has the responsibility to accept the following:
* single-line input from the keyboard with a return following each line of input
* multiple lines of input from the keyboard via a cut and paste operation such as from a file
* Redirected input from a file that may have commands spanning multiple lines.
The problem encapsulated clearly for me is this: Right now my front door uses Scanner. It can handle single line input, as in, type something, hit return, and watch the fireworks. however, if I decide to redirect from a file that contains multiple lines of data or cut-n-paste from the file containing multiple lines of data, my program will have a brain fart and see only each line as input. For example:
Ex. 1
Keyboard Input:  This is my input (followed by carriage return)
Response from the program: Nice job
Ex. 2
File Input:  Line 1
                Line 2
                Line 3
Response from the program:  I don't understand, I have only partial data
                                         I don't understand, I have only partial data
                                         I don't understand, I have only partial data
Ex. 3
Cut-n-Paste: Line 1
                  Line 2
                  Line 3
Response from the program:  I don't understand, I have only partial data
                                         I don't understand, I have only partial data
                                         I don't understand, I have only partial dataIt is clear I provided valid data in all three examples. The only difference is I chose to split that data across multiple lines where each line is followed by a carriage return. The program had a brain fart and barfed all over my data. This is not what I want to happen.
So then how can I do this?
Edited by: 832844 on Feb 24, 2011 10:37 AM

Similar Messages

Maybe you are looking for