1

Getoptクラス エラー メッセージを文字列に格納する必要があります。

通常、そのエラーは標準エラー コンソールに表示されます。

どうすればいいですか?

4

1 に答える 1

1

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
于 2013-09-26T08:52:48.337 に答える