0

我已经编写了一个代码来存根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

4

1 回答 1

0

既然你已经有了一个ByteArrayOutputStream,你应该尝试这样的事情:

BufferedReader reader = new BuffererdBeader( new InputStreamReader(
                            new ByteArrayInputStream( outcontent.toByteArray() ) ) );
于 2017-07-05T15:05:21.433 回答