How can we redirect a Java program console output to multiple files?

后端 未结 5 412
忘掉有多难
忘掉有多难 2020-11-28 20:36

How can we redirect the eclipse console output to a file? I can:

  1. Run Configuration->Commons->Select a file.
  2. Use
5条回答
  •  -上瘾入骨i
    2020-11-28 20:52

    You can set the output of System.out programmatically by doing:

    System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("/location/to/console.out")), true));
    

    Edit:

    Due to the fact that this solution is based on a PrintStream, we can enable autoFlush, but according to the docs:

    autoFlush - A boolean; if true, the output buffer will be flushed whenever a byte array is written, one of the println methods is invoked, or a newline character or byte ('\n') is written

    So if a new line isn't written, remember to System.out.flush() manually.

    (Thanks Robert Tupelo-Schneck)

提交回复
热议问题