I need to store Getopt
class error message to a string.
Normally that error will be printed in standard error console.
How would I do this?
I need to store Getopt
class error message to a string.
Normally that error will be printed in standard error console.
How would I do this?
You may be able to use System.setOut
and/or System.setErr
.
As an example:
static void printMessage()
{
System.out.println("Hello");
}
public static void main(String[] args) throws Exception
{
// ByteArrayOutputStream = in-memory output stream
OutputStream output = new ByteArrayOutputStream();
PrintStream oldStream = System.out; // store the current output stream
System.setOut(new PrintStream(output)); // change the output stream
printMessage(); // call function that prints to System.out
System.setOut(oldStream); // restore the old output stream
System.out.println("Output from stream: " + output.toString());
}
The above will lead to only the following output:
Output from stream: Hello