Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use PipedInputStream and PipedOutputStream.</p> <pre><code>//create pairs of Piped input and output streasm for std out and std err final PipedInputStream outPipedInputStream = new PipedInputStream(); final PrintStream outPrintStream = new PrintStream(new PipedOutputStream( outPipedInputStream)); final BufferedReader outReader = new BufferedReader( new InputStreamReader(outPipedInputStream)); final PipedInputStream errPipedInputStream = new PipedInputStream(); final PrintStream errPrintStream = new PrintStream(new PipedOutputStream( errPipedInputStream)); final BufferedReader errReader = new BufferedReader( new InputStreamReader(errPipedInputStream)); final PrintStream originalOutStream = System.out; final PrintStream originalErrStream = System.err; final Thread writingThread = new Thread(new Runnable() { @Override public void run() { try { System.setOut(outPrintStream); System.setErr(errPrintStream); // You could also set the System.in here using a // PipedInputStream DoSomething(); // Even better would be to refactor DoSomething to accept // PrintStream objects as parameters to replace all uses of // System.out and System.err. DoSomething could also have // an overload with DoSomething() calling: DoSomething(outPrintStream, errPrintStream); } finally { // may also want to add a catch for exceptions but it is // essential to restore the original System output and error // streams since it can be very confusing to not be able to // find System.out output on your console System.setOut(originalOutStream); System.setErr(originalErrStream); //You must close the streams which will auto flush them outPrintStream.close(); errPrintStream.close(); } } // end run() }); // end writing thread //Start the code that will write into streams writingThread.start(); String line; final List&lt;String&gt; completeOutputStreamContent = new ArrayList&lt;String&gt;(); while ((line = outReader.readLine()) != null) { completeOutputStreamContent.add(line); } // end reading output stream final List&lt;String&gt; completeErrorStreamContent = new ArrayList&lt;String&gt;(); while ((line = errReader.readLine()) != null) { completeErrorStreamContent.add(line); } // end reading output stream </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload