I am invoking a function that is printing some string in my console/standard output. I need to capture this string. I cannot modify the function that is doing the printing, nor change runtime behavior through inheritance. I am unable to find any pre-defined methods that will allow me to do this.
Does the JVM store a buffer of printed contents?
Does anyone know of a Java method that will aid me?
解决方案
You could temporarily replace System.err or System.out with a stream that writes to string buffer.