0

I have a method that has system.out and it's used in main. How can I intercept that print and assign it to a String variable? I also do not want it to be printed.

public void print()
System.out.println("hello");

---main---
print(); // need to intercept this 
String str = print(); // need assign the contents of the print() to str and not show the contents of print in the console

Edit: Due to some restrictions, I cannot create .txt and I cannot change the code of the method. I need to make all the changes in main

  • 1
    set it to a `PrintWriter` pointing to a `StringWriter` – dan1st Nov 06 '20 at 13:31
  • @dan1st Sorry can you elaborate with code? –  Nov 06 '20 at 13:34
  • 1
    If you possibly can, change the code so as not to use any global state. (Otherwise, `System.setOut`.) – Tom Hawtin - tackline Nov 06 '20 at 13:38
  • @TomHawtin-tackline which code? I'm not sure what do you mean by a global state –  Nov 06 '20 at 13:41
  • @Ernest The code which uses `System.out`. Replace it with code that will print to a passed in `PrintStream` object (or object of similar type). / *Global state* is usually in the form of `static` variables hidden somewhere. It just means variable state that is not local. – Tom Hawtin - tackline Nov 07 '20 at 04:53
  • I want to redirect to my own PrintWriter. The question that "already has answers" is entitled as directing to a string, and leads with a discussion about void return method, neither of which are relevant to my question. This question is phrased in a way that is more helpful for me, the other question is not. That's maybe why _this_ question comes up top of the search results in Google. – John Aug 07 '21 at 16:21

1 Answers1

2

You can call System.setOut() to change the PrintStream used by System.out. You probably also want to call setErr() to the same PrintStream.

To illustrate, let us use a standard "Hello World" program.

public static void main(String[] args) {
    System.out.println("Hello World");
}

Output

Hello World

We now replace the output print stream with a print stream that captures all the output in a buffer. We do keep a copy of the original print stream so we can print something for real at the end.

public static void main(String[] args) {
    PrintStream oldSysOut = System.out;
    ByteArrayOutputStream outBuf = new ByteArrayOutputStream();
    try (PrintStream sysOut = new PrintStream(outBuf, false, StandardCharsets.UTF_8)) {
        System.setOut(sysOut);
        System.setErr(sysOut);
        
        // Normal main logic goes here
        System.out.println("Hello World");
    }
    String output = new String(outBuf.toByteArray(), StandardCharsets.UTF_8);
    oldSysOut.print("Captured output: \"" + output + "\"");
}

Output

Captured output: "Hello World
"

As can be seen here, all the output was captured, including the linebreak from the println() call.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • any idea why the normal System.outprintln() doesn't work afterwards? –  Nov 21 '20 at 04:08