21

I am invoking a function that is printing some string in my console/standard output. I need to capture this string. I cannot modify the function that is doing the printing, nor change runtime behavior through inheritance. I am unable to find any pre-defined methods that will allow me to do this.

Does the JVM store a buffer of printed contents?

Does anyone know of a Java method that will aid me?

Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90
Shailesh Tainwala
  • 6,299
  • 12
  • 58
  • 69
  • 1
    this seems to be very hacky, try sth. else instead, another method or so... – Tobias Mar 22 '11 at 10:12
  • Possible Duplicate of http://stackoverflow.com/questions/4334808/how-could-i-read-java-console-output-into-a-string-buffer – Kartik Domadiya Mar 22 '11 at 10:25
  • what 'console/standard output' printing ? Note that `System.console().writer().print()` printings will not be redirected with `System.setOut(myPrintStream);` – oliholz Mar 22 '11 at 11:08

3 Answers3

34

You can redirect the standard output by calling

System.setOut(myPrintStream);

Or - if you need to log it at runtime, pipe the output to a file:

java MyApplication > log.txt

Another trick - if you want to redirect and can't change the code: Implement a quick wrapper that calls your application and start that one:

public class RedirectingStarter {
  public static void main(String[] args) {
    System.setOut(new PrintStream(new File("log.txt")));
    com.example.MyApplication.main(args);
  }
}
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
5

You could temporarily replace System.err or System.out with a stream that writes to string buffer.

Ingo
  • 36,037
  • 5
  • 53
  • 100
4
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

public class RedirectIO
{

    public static void main(String[] args)
    {
        PrintStream orgStream   = null;
        PrintStream fileStream  = null;
        try
        {
            // Saving the orginal stream
            orgStream = System.out;
            fileStream = new PrintStream(new FileOutputStream("out.txt",true));
            // Redirecting console output to file
            System.setOut(fileStream);
            // Redirecting runtime exceptions to file
            System.setErr(fileStream);
            throw new Exception("Test Exception");

        }
        catch (FileNotFoundException fnfEx)
        {
            System.out.println("Error in IO Redirection");
            fnfEx.printStackTrace();
        }
        catch (Exception ex)
        {
            //Gets printed in the file
            System.out.println("Redirecting output & exceptions to file");
            ex.printStackTrace();
        }
        finally
        {
            //Restoring back to console
            System.setOut(orgStream);
            //Gets printed in the console
            System.out.println("Redirecting file output back to console");

        }

    }
}
oliholz
  • 7,447
  • 2
  • 43
  • 82
Kartik Domadiya
  • 29,868
  • 19
  • 93
  • 104