Getopt
クラス エラー メッセージを文字列に格納する必要があります。
通常、そのエラーは標準エラー コンソールに表示されます。
どうすればいいですか?
System.setOut
および/またはを使用できる場合がありますSystem.setErr
。
例として:
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());
}
上記は、次の出力のみにつながります。
Output from stream: Hello