我已经编写了一个代码来存根System.out.println
并将对象传递给BuffredReader
它。我的问题是如何恢复BufferedReader
对象?
import java.io.*;
class Test {
public static void main(String args[]) throws IOException {
// stubbing the default print statement
ByteArrayOutputStream outcontent = new ByteArrayOutputStream();
System.setOut(new PrintStream(outcontent);
//createing a BufferedReader obj and passing to print
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(br);
//trying to get the value from the outcontent and
// but i need to serialise this to BufferedReader
System.err.println(outContent.toString());
}
}
我试图使用这个答案,但我得到了错误java.io.StreamCorruptedException: invalid stream header: 6A617661