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

JavaEclipse

Java Problem Overview


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

  1. Run Configuration->Commons->Select a file.
  2. Use System.setOut(PrintStream) and System.setErr(PrintStream).

The problem with 1) is that I need to record console output to different files instead of one file.

The problem with 2) is that it only stores console output generated by System.out.println() or stacktrace of an exception thrown. It does not capture other kind of outputs e.g. log4j warnings etc.

How can we redirect console output to different files programmatically?

Java Solutions


Solution 1 - Java

Go to run as and choose Run Configurations -> Common and in the Standard Input and Output you can choose a File also.

Solution 2 - Java

You could use a "variable" inside the output filename, for example:

/tmp/FetchBlock-${current_date}.txt

current_date: > Returns the current system time formatted as yyyyMMdd_HHmm. An optional argument can be used to provide alternative formatting. The argument must be valid pattern for java.util.SimpleDateFormat.

Or you can also use a system_property or an env_var to specify something dynamic (either one needs to be specified as arguments)

Solution 3 - Java

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][1]:

> 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) [1]: http://docs.oracle.com/javase/6/docs/api/java/io/PrintStream.html#PrintStream%28java.io.OutputStream,%20boolean%29

Solution 4 - Java

To solve the problem I use ${string_prompt} variable. It shows a input dialog when application runs. I can set the date/time manually at that dialog.

  1. Move cursor at the end of file path. enter image description here

  2. Click variables and select string_prompt enter image description here

  3. Select Apply and Run enter image description here enter image description here

Solution 5 - Java

We can do this by setting out variable of System class in the following way

System.setOut(new PrintStream(new FileOutputStream("Path to output file"))). Also You need to close or flush 'out'(System.out.close() or System.out.flush()) variable so that you don't end up missing some output.

Source : http://xmodulo.com/how-to-save-console-output-to-file-in-eclipse.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAli ShahView Question on Stackoverflow
Solution 1 - JavaShervin AsgariView Answer on Stackoverflow
Solution 2 - Javamike32bView Answer on Stackoverflow
Solution 3 - JavaIan2thedvView Answer on Stackoverflow
Solution 4 - JavaiOS-Developer84View Answer on Stackoverflow
Solution 5 - JavadrpView Answer on Stackoverflow