title: PrintWriter flush not working?
body: <p>I'm trying to get the following code working so that I can use it somewhere else.</p>
  
  <p>Effectively, it (is supposed to) start another process, run python in it, and feed python some commands. However, in practice, unless I close the stream to that process, the the python commands are never sent. I thought flush() was supposed to force this to happen, but it does not appear to be working. Could anyone provide any insight as to why flush() may not be working and what I could do to avoid this? Thanks.</p>
  
  <p>Note that if I call close() the command is sent. However, I want to be able to send more commands after this one command, so using close() here seems unacceptable. (I'll eventually close() everything)</p>
  
  <pre><code>import java.io.BufferedWriter;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.OutputStream;
  import java.io.OutputStreamWriter;
  import java.io.PrintWriter;
  import java.util.Scanner;
  
  public class Foo {
   public static void main(String[] args) throws IOException {
    Process cmd = Runtime.getRuntime().exec("python");
  
    InputStream inStream = cmd.getInputStream();
    Thread stdout = new Thread(new stdOutReader(inStream));
    stdout.start();
  
    InputStream errStream = cmd.getErrorStream();
    Thread stderr = new Thread(new stdOutReader(errStream));
    stderr.start();
  
    OutputStream outStream = cmd.getOutputStream();
    OutputStreamWriter os = new OutputStreamWriter(outStream);
    PrintWriter pWriter = new PrintWriter(outStream, true);
    pWriter.println("print \"Testing..\"");
  
    pWriter.flush();
    int x = 0;
    while (x &lt; 100){
        //Do stuff here (will not be an infinite loop in actual code)
    }
  
    pWriter.close();
  
  }
  private static class stdOutReader implements Runnable{
     InputStream inStream;
  
     public stdOutReader(InputStream inStream){
         this.inStream = inStream;
     }
  
     public void run() {
         InputStreamReader reader = new InputStreamReader(this.inStream);
         Scanner scan = new Scanner(reader);
         while (scan.hasNext()) {
            System.out.println(scan.next());
            System.out.flush();
         }
     }
   }
  }
  </code></pre>