我试图使用System.setOut将System.out重定向到一个String,它接受一个PrintStream.有没有办法将StringWriter转换为Stream,以便我可以将它传递给setOut?
Yuv*_*dam 26
你不能完全这样做,因为StringWriter是Writer,而不是Stream.但你可以这样做:
// create a ByteArray stream, which will be wrapped by a PrintStream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
System.setOut(ps);
// print whatever you got
String result = baos.toString();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14412 次 |
| 最近记录: |