aspose file tools
The moose likes Java in General and the fly likes Simulate value into BufferedReader Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Simulate value into BufferedReader" Watch "Simulate value into BufferedReader" New topic
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
    
    1

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
    
    1

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
    
    2

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
    
    1

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
    
    2

Absolutely. However anybody who wants to use the Process class should read this classic article: When Runtime.exec() won't.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simulate value into BufferedReader
 
Similar Threads
InputStreamReader
Without KeyBoard input assiging value to local variable
How can I....
Internet Programming (Java) Need Help
Sending Serialized Objects to A Server And Displaying the contents