开发者

Get console text in java

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

System.out.print("blabla");

开发者_如何学Python?


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜