java获取System.out输出内容

ByteArrayOutputStream baoStream = new ByteArrayOutputStream(1024);
PrintStream cacheStream = new PrintStream(baoStream);
PrintStream oldStream = System.out;
System.setOut(cacheStream);//不打印到控制台
System.out.println("System.out.println:test");
String strMsg = baoStream.toString();
System.setOut(oldStream);//还原到控制台输出
System.out.println(strMsg);

你可能感兴趣的:(控制台)