1

Im trying to figure out how to store all of the System.out.println to a List of string, like anytime if System.out.println is called it will be store as an element of List.

I already know that we can capture the System.out.print using System.setOut().

Thank you in advance.

Betoobb
  • 37
  • 1
  • 8
  • Does this answer your question? [Redirect console output to string in Java](https://stackoverflow.com/questions/8708342/redirect-console-output-to-string-in-java) – Charlie Armstrong Dec 15 '20 at 19:03
  • hmm I actually want to capture it as an element of List – Betoobb Dec 15 '20 at 19:10
  • Can't you just capture it to a String, and then post-process that to whatever form you ultimately want? Do you need to do this continuously, or can the conversion be done just one time, once you know that no further output needs to be captured? – CryptoFool Dec 15 '20 at 19:17
  • I just realized that I might have missed what you're asking for. You want to capture the output from `println` but not from `print`? If so, you should know that there are 10 overloads of the `println` function. You'd have to overload as many of them as you want to capture output from, as they all call `print` in their current implementations. If you really need to do this, why not replace the call to `println` with a method or methods in your own code and leave System.out alone? - hopefully, you really want to capture everything, and can use @IsmailDurmaz's solution, which seems great. – CryptoFool Dec 15 '20 at 19:31
  • yes I want to capture everything from `println`. I'm developing an UI based on a java application. So my intent is to capture everything from the system.out and transfer it to the Frontend. Thanks for all of your support. I will try out @IsmailDurmaz's solution – Betoobb Dec 15 '20 at 19:45

2 Answers2

8

System.out.println method has print and newLine methods. There is no way to capture only System.out.println method, you should capture all System.out.print methods with changing System.out variable.

System.out.println jdk implementation:

    public void println(String x) {
        synchronized (this) {
            print(x);
            newLine();
        }
    }

Text collector output stream class for capturing System.out.print content:

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class TextCollector extends OutputStream {
    private final List<String> lines = new ArrayList<>();
    private StringBuilder buffer = new StringBuilder();

    @Override
    public void write(int b) throws IOException {
        if (b == '\n') {
            lines.add(buffer.toString());
            buffer = new StringBuilder();
        } else {
            buffer.append((char) b);
        }
    }

    public List<String> getLines() {
        return lines;
    }
}

Example test implementation:

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) {
        // store current output
        PrintStream tmpOut = System.out;

        // change stream into text collector
        TextCollector textCollector = new TextCollector();
        System.setOut(new PrintStream(textCollector));

        // collect lines
        System.out.println("Test-1");
        System.out.println("Test-2");

        // print lines to console
        System.setOut(tmpOut);
        List<String> lines = textCollector.getLines();
        for (String line : lines) {
            System.out.println(line);
        }
    }
}
Ismail Durmaz
  • 2,521
  • 1
  • 6
  • 19
1

Here is a simple way to do it. Just override as many types as you need to print for println.

public class MyPrintStreamDemo extends PrintStream {
    static List<Object> list = new ArrayList<>();
    static PrintStream orig = System.out; 
    public MyPrintStreamDemo() {
        // any print method not overridden will print normally.
        // If you want to suppress non-overridden output
        // set the first argument below to PrintStream.nullOutputStream()
        super(System.out, true);
    }
    
    
    public static void main(String[] args) {
        System.setOut(new MyPrintStreamDemo());
        System.out.println("This is a test");
        System.out.println("And so is this");
        System.out.println(10);
        System.out.println(20.222);
        System.out.println(List.of(1,2,3,4));
        orig.println(list);
    }
    
    
    public void println(String s) {
        list.add(s);
    }
    public void println(double s) {
        list.add(s);
    }
    public void println(int s) {
        list.add(s);
    }
    public void println(Object s) {
        list.add(s);
    }
}

The ArrayList contains the following:

[This is a test, And so is this, 10, 20.222, [1, 2, 3, 4]]
WJS
  • 36,363
  • 4
  • 24
  • 39