You do not need to override all methods in PrintWriter , since all printXyz methods printXyz delegated to the underlying write methods of the Writer API.
Although: PrintWriter has a PrintWriter(Writer out) constructor. Therefore, you only need to implement the new Writer with _one_method:
public class MultiWriter implements Writer { private List<Writer> delegates; public MultiWriter(List<Writer> delegates){ this.delegates = delegates; } public void write(char cbuf[], int off, int len) throws IOException { for(Writer w: delegates){ w.writer(cbuf, off, len); } } }
use it as follows:
PrintWriter myPrinter = new PrintWriter(new MultiWriter(listOfDelegates)); myPrintWriter.println("Hello World!");
This will take care of Writers .
You can do the same trick using OutputStream . You can also simply implement MultiOutputStream , skip MultiWriter and use the delegation chain PrintWriter->OutputStreamWriter->MultiOutputStream . This will be just one method in one class to implement, and you will only get PrintWriter , Writer and OutputStream .
source share