我试图从以下代码块中的PrintStram读取(将传入的数据附加到本地String):
System.out.println("Starting Login Test Cases...");
out = new PrintStream(new ByteArrayOutputStream());
command_feeder = new PipedWriter();
PipedReader in = new PipedReader(command_feeder);
main_controller = new Controller(in, out);
for(int i = 0; i < cases.length; i++)
{
command_feeder.write(cases[i]);
}
Run Code Online (Sandbox Code Playgroud)
main_controller将为其out(PrintStream)写一些字符串,那么我怎么能从这个PrintStream中读取,假设我无法更改Controller类中的任何代码?提前致谢.
简单地说:你做不到.PrintStream用于输出,读取数据,您需要一个InputStream(或任何子类).
您已经有了ByteArrayOutputStream.最容易做的是:
// ...
ByteArrayOutputStream baos = new ByteArrayOutputStream();
out = new PrintStream(baos);
// ...
ByteArrayInputStream in = new ByteArrayInputStream(baos.toByteArray());
// use in to read the data
Run Code Online (Sandbox Code Playgroud)