我有一个Java程序,可以将一些文本输出到控制台.它使用print,println和其他一些方法来做到这一点.
在程序结束时,我想读取控制台中的所有文本并将其复制到String缓冲区中.我怎么能用Java做到这一点?我需要阅读stdout和stderr独立.
aio*_*obe 18
好的,这是一个有趣的问题.似乎并不是一种优雅的方法,可以立即解决所有PrintStream方法问题.(不幸的是没有FilterPrintStream.)
我确实编写了一个丑陋的基于反射的解决方法(不要在生产代码中使用我想:)
class LoggedPrintStream extends PrintStream {
final StringBuilder buf;
final PrintStream underlying;
LoggedPrintStream(StringBuilder sb, OutputStream os, PrintStream ul) {
super(os);
this.buf = sb;
this.underlying = ul;
}
public static LoggedPrintStream create(PrintStream toLog) {
try {
final StringBuilder sb = new StringBuilder();
Field f = FilterOutputStream.class.getDeclaredField("out");
f.setAccessible(true);
OutputStream psout = (OutputStream) f.get(toLog);
return new LoggedPrintStream(sb, new FilterOutputStream(psout) {
public void write(int b) throws IOException {
super.write(b);
sb.append((char) b);
}
}, toLog);
} catch (NoSuchFieldException shouldNotHappen) {
} catch (IllegalArgumentException shouldNotHappen) {
} catch (IllegalAccessException shouldNotHappen) {
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
......可以像这样使用:
public class Test {
public static void main(String[] args) {
// Create logged PrintStreams
LoggedPrintStream lpsOut = LoggedPrintStream.create(System.out);
LoggedPrintStream lpsErr = LoggedPrintStream.create(System.err);
// Set them to stdout / stderr
System.setOut(lpsOut);
System.setErr(lpsErr);
// Print some stuff
System.out.print("hello ");
System.out.println(5);
System.out.flush();
System.err.println("Some error");
System.err.flush();
// Restore System.out / System.err
System.setOut(lpsOut.underlying);
System.setErr(lpsErr.underlying);
// Print the logged output
System.out.println("----- Log for System.out: -----\n" + lpsOut.buf);
System.out.println("----- Log for System.err: -----\n" + lpsErr.buf);
}
}
Run Code Online (Sandbox Code Playgroud)
结果输出:
hello 5
Some error
----- Log for System.out: -----
hello 5
----- Log for System.err: -----
Some error
Run Code Online (Sandbox Code Playgroud)
(注意,该out字段FilterOutputStream是受保护和记录的,因此它是API的一部分:-)
程序运行完毕后,您无法执行此操作.您需要在程序开始写输出之前执行此操作.
有关如何替换stdout和stderr的详细信息,请参阅此文章.核心呼叫是System.setOut()和System.setErr().
| 归档时间: |
|
| 查看次数: |
21866 次 |
| 最近记录: |