| Author |
Simulate value into BufferedReader
|
Anjali Lamba
Greenhorn
Joined: Feb 02, 2011
Posts: 11
|
|
In Java How do I simulate autowrite to a Buffered reader prompt ?
For example, the BufferedReader says
Enter the name:
here the name Java shouldbe accepted like
Enter the name:Java
through the Java code itself
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3049
|
|
You could use a pipe. First create a new PipedReader and a PipedWriter like this:
Now, you can wrap your BufferedReader around the PipedReader. Anything you write in your PipedWriter should appear in your BufferedReader.
|
 |
Anjali Lamba
Greenhorn
Joined: Feb 02, 2011
Posts: 11
|
|
I understood your solution(commented in my code above).
Assume running the above program prompts me to give a name in a cmd prompt. Now how do I enter the name through the code?
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3049
|
|
I don't think you can. The command prompt is just that: it provides a means for a human to provide input. If you want a process to provide input, you can either use a pipe within the same process, or you can redirect standard input to accept input from another process' output.
What is the reason you want to do this? Maybe we can suggest an alternative solution.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
When you have a reference to a Process, you can call its getOutputStream() method to get an OutputStream. What can you do with that? Well:
The API Documentation wrote:Output to the stream is piped into the standard input stream of the process represented by this Process object.
So you can send data to the stdin of the process you ran there, which is what you were asking about.
However in your particular example I don't think that rundll32 is going to be asking for any input. If I'm not mistaken, rundll32 starts up another process which runs whatever you told it to start up, and if I'm also not mistaken, your Java code doesn't have any connection with that process.
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3049
|
|
|
Hmm that's pretty cool, I'll have to take a look at that class as well.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
|
Absolutely. However anybody who wants to use the Process class should read this classic article: When Runtime.exec() won't.
|
 |
 |
|
|
subject: Simulate value into BufferedReader
|
|
|