Reading System.out.println (Text) from another class

Basically I am making a class that is supposed to read a whole chain from another class.

Another class is System.out.printing integers in the following order:

i i i i
i i i i
... etc.

So I basically want to create a String file or something else that I can read this output from the first class.

I'm pretty lost, I don't know if I should create a scanner (System.in), and if so, what to do with it.

I think this is a fairly open question, so apologize in advance.

+3
source share
3 answers

System.out - , System.out - () .

, " " , . , - :

public void print(int[] values) {
  // ...
  System.out.println(result);
}

public String prepareResult(int[] values) {
  // ...
  return result;
}

public void print(int[] values) {
  System.out.println(prepareResult(values));
}

prepareResult, .

+1

, , , , " " .

" " , .

  • System.setOut() stdout .
  • : , . " " -, ( ) .

API, , .

  • new Throwable(). getStackTrace() [0].getClassName() , .
  • outptut .

public class FilterOutputStream extends OutputStream {
    private OutputStream payload;
    public FilterOutputStream(OutputStream payload) {
        this.payload = payload;
    }

    public void write(int b) throws IOException {
        if (isSpecialClass()) {
            specialWrite(b);
        } else {
            payload.write(b);
        }
    }
    public void flush() throws IOException {
          payload.flush();
    }
    public void close() throws IOException {
         payload.close();
    }
    public boolean isSpecialClass() {
            return "MySpecialClass".equals(new Throwable().getStackTrace()[0].getClassName());
    }

}

, , , , . , .

+1

System.setOut() System.setErr() ( ).

0

Source: https://habr.com/ru/post/1775628/


All Articles