title: java: how to both read and write to & from process through pipe (stdin/stdout)
body: <p>(i'm new to java)
  I need to start a process and receive 2 or 3 handles: for STDIN, STDOUT, (and STDERR), so I can write input to the process and receive its output, the same way command line pipes behave (e.g. "grep")</p>
  
  <p>in Python this is acheived with the following code: </p>
  
  <pre><code>from subprocess import Popen, PIPE
  p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE)
  (child_stdin, child_stdout) = (p.stdin, p.stdout)
  child_stdin.write('Yoram Opposum\n')
  child_stdin.flush()
  child_stdout.readlines()
  </code></pre>
  
  <p>What's the Java equivalent??</p>
  
  <p>I've tried so far</p>
  
  <pre><code>Process p = Runtime.getRuntime().exec(cmd);
  BufferedReader inp = new BufferedReader( new InputStreamReader(p.getInputStream()) );
  BufferedWriter out = new BufferedWriter( new OutputStreamWriter(p.getOutputStream()) );
  out.write( "Some Text!\n\n" );
  out.flush();
  line = inp.readLine();
  print("response1: " + line );   // that's ok
  out.write( "Second Line...\n" );
  out.flush();
  line = inp.readLine();
  print("response2: " + line );    // returns an empty string, if it returns,,,
  inp.close();
  out.close();
  </code></pre>
  
  <p>BTW the first try works only with \n\n, but doesn't work with single \n (why?)</p>
  
  <p>the following code works, but all input is given in advance, not the behavior i'm looking for:</p>
  
  <pre><code>out.write( "Aaaaa\nBbbbbb\nCcccc\n" );
  out.flush();
  line = inp.readLine();
  print("response1: " + line );
  line = inp.readLine();
  print("response2: " + line );
  line = inp.readLine();
  print("response3: " + line );
  line = inp.readLine();
  print("response4: " + line );
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>response1: AAAAA
  response2: 
  response3: bbbbbb
  response4: 
  </code></pre>
  
  <p>the process being run looks like that:</p>
  
  <pre><code>s = sys.stdin.readline()
  print s.upper()
  s = sys.stdin.readline()
  print s.lower()
  </code></pre>