Click here to Skip to main content
15,886,100 members
Articles / General Programming / File
Tip/Trick

A quick and easy way to direct Java System.out to File and to Console

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
17 Jan 2012CPOL 154.8K   5   7
A quick and easy way to direct Java System.out to File and to Console.

The following code demonstrates a very easy and simple way to redirect Java System.out and System.err streams to both a File and Console.


For some reason, Googling for "Java stdout redirect to File and Console" does not result in any easy answers on the first few pages. The answer being so insanely simple, I decided to quickly write the method here, in the hopes that future programmers won't have to spend more than a few minutes on such an issue.


The issue being, of course, how does one use the methods in System.out or System.err to print to both a file and the console without having to resort to ridiculous extremes? In short, the answer is to wrap multiple OutputStreams into a single OutputStream class implementation and construct a PrintStream instance on this implementation, and then set this PrintStream implementation on System.


So first define your MultiOutputStream class:


Java
public class MultiOutputStream extends OutputStream
{
	OutputStream[] outputStreams;
	
	public MultiOutputStream(OutputStream... outputStreams)
	{
		this.outputStreams= outputStreams; 
	}
	
	@Override
	public void write(int b) throws IOException
	{
		for (OutputStream out: outputStreams)
			out.write(b);			
	}
	
	@Override
	public void write(byte[] b) throws IOException
	{
		for (OutputStream out: outputStreams)
			out.write(b);
	}

	@Override
	public void write(byte[] b, int off, int len) throws IOException
	{
		for (OutputStream out: outputStreams)
			out.write(b, off, len);
	}

	@Override
	public void flush() throws IOException
	{
		for (OutputStream out: outputStreams)
			out.flush();
	}

	@Override
	public void close() throws IOException
	{
		for (OutputStream out: outputStreams)
			out.close();
	}
}

Next construct your PrintStream instance and set it on System:


Java
try
{
	FileOutputStream fout= new FileOutputStream("stdout.log");
	FileOutputStream ferr= new FileOutputStream("stderr.log");
	
	MultiOutputStream multiOut= new MultiOutputStream(System.out, fout);
	MultiOutputStream multiErr= new MultiOutputStream(System.err, ferr);
	
	PrintStream stdout= new PrintStream(multiOut);
	PrintStream stderr= new PrintStream(multiErr);
	
	System.setOut(stdout);
	System.setErr(stderr);
}
catch (FileNotFoundException ex)
{
	//Could not create/open the file
}

Now you can go bananas and use System.out or System.err to write whatever you like, and it will be written to both the console and a file:


Java
System.out.println("Holy Rusty Metal Batman! I can't believe this was so simple!");
System.err.println("God I hate you Robin.");

Goodbye, have a nice time!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Nokia
Germany Germany
Writing C++ and Java Software like its 1999.

Comments and Discussions

 
QuestionGreat article, just what I was looking for Pin
GeVanCo13-Jul-18 19:08
GeVanCo13-Jul-18 19:08 
GeneralMy vote of 3 Pin
family5215-Feb-13 15:34
family5215-Feb-13 15:34 
GeneralReason for my vote of 5 Very simple and straight forward sol... Pin
All Time Programming6-Feb-12 19:07
All Time Programming6-Feb-12 19:07 
General@Member 3896609, If we use TeeOutputStream then our applicat... Pin
All Time Programming6-Feb-12 19:05
All Time Programming6-Feb-12 19:05 
GeneralRe: Agreed. If you are using the Commons IO library then, by all... Pin
Lodewijk Pool7-Feb-12 0:47
Lodewijk Pool7-Feb-12 0:47 
GeneralThe implementation of MultiOutputStream is unnecessary in yo... Pin
Member 38966092-Feb-12 1:23
Member 38966092-Feb-12 1:23 
QuestionNice article Pin
devcovato17-Jan-12 5:49
devcovato17-Jan-12 5:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.