1

Is there a way to retrieve output from the console that has been outputted by:

System.out.print("blabla");

?

DVK
130k33 gold badges220 silver badges337 bronze badges
asked Feb 5, 2011 at 10:08
2
  • 3
    Where do you want to retrieve it? Do you want to retrieve it in the program in which you did the print? Or do you want to read it in another program? Commented Feb 5, 2011 at 10:13
  • I wanted to retrieve it by another part of the program, to make some sort of generic tester. By viewing the answers, I've found a better solution for this problem by making the Environment visible for the tester Commented Feb 7, 2011 at 13:30

3 Answers 3

5

If you want to be able to see what you already wrote to the console, you need to write your own PrintStream implementation that simply wraps an existing PrintStream, stores whatever it is supposed to write and then delegates (all the methods) to the wrapped (original) PrintStream to do the actual job. How you store the messages entirely depends on your needs (store only the last written String, store a map of timestamp -> String or whatever). Once you have this, you can replace System.out with your own implementation (via System.setOut()):

public class RememberAllWrittenTextPrintStream extends PrintStream {
 private static final String newLine = System.getProperty("line.separator");
 private final StringBuffer sb = new StringBuffer();
 private final PrintStream original;
 public RememberAllWrittenTextPrintStream(PrintStream original) {
 this.original = original;
 }
 public void print(double d) {
 sb.append(d);
 original.print(d);
 }
 public void print(String s) {
 sb.append(s);
 original.print(s);
 }
 public void println(String s) {
 sb.append(s).append(newLine);
 original.println(s);
 }
 public void println() {
 sb.append(newLine);
 original.println();
 }
 public void printf(String s, Object... args) {
 sb.append( String.format(s, args) );
 original.printf(s, args);
 }
 // .....
 // the same for ALL the public methods in PrintStream....
 // (your IDE should help you easily create delegates for the `original` methods.)
 public String getAllWrittenText() {
 return sb.toString();
 }
}

You also may need to take care of thread-safety (StringBuffer is thread-safe, but you may need more than this).

Once you have the above, you can:

RememberAllWrittenTextPrintStream ps
 = new RememberAllWrittenTextPrintStream(System.out);
System.setOut(ps);
System.out.print("bla");
System.out.print("bla");
ps.getAllWrittenText(); // should now return "blabla"

EDIT: added the println() implementations using a platform-independent newLine.

answered Feb 5, 2011 at 10:44
Sign up to request clarification or add additional context in comments.

1 Comment

I already was afraid the answer would be not just a some builtin functionality, I found a cleaner solution for my problem. Thanks
0

If I understood you, you can start a process from java and read it's output like this:

ProcessBuilder builder = new ProcessBuilder("/bin/bash");
builder.redirectErrorStream(true);
Process process = builder.start();
OutputStream stdin = process.getOutputStream ();
InputStream stderr = process.getErrorStream ();
InputStream stdout = process.getInputStream ();
BufferedReader reader = new BufferedReader (new InputStreamReader(stdout));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
String input = scan.nextLine();
...

If you need more sophisticated control of any process (accessed by pid, name etc.) this is really good library: Java Service Wrapper

answered Feb 5, 2011 at 10:19

Comments

0

I'm unaware of your exact requirement but, If you would like to write output to multiple destinations, use any logging frameworks. Logger's allow more control on writing the output. Please see the below link for more information and it's a best practice too.

http://logging.apache.org/index.html and http://www.javaworld.com/javaworld/jw-12-2004/jw-1220-toolbox.html

answered Feb 5, 2011 at 14:47

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.