Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

, 17 Jan 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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:

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:

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)

Share

About the Author

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

Comments and Discussions

 
GeneralMy vote of 3 Pinmemberfamily5215-Feb-13 16:34 
GeneralReason for my vote of 5 Very simple and straight forward sol... PinmemberAll Time Programming6-Feb-12 20:07 
General@Member 3896609, If we use TeeOutputStream then our applicat... PinmemberAll Time Programming6-Feb-12 20:05 
GeneralRe: Agreed. If you are using the Commons IO library then, by all... PinmemberLodewijk Pool7-Feb-12 1:47 
GeneralThe implementation of MultiOutputStream is unnecessary in yo... PinmemberMember 38966092-Feb-12 2:23 
QuestionNice article Pinmemberdevcovato17-Jan-12 6:49 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.1 | Last Updated 17 Jan 2012
Article Copyright 2012 by Lodewijk Pool
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid