I think you would any ways need to have different instances to write to different files. Try to modularize your code- Add a method to get a PrintWriter object for the file you pass along with the method suggested by Wouter.
Also you need not specifically create FileWriter object. You can have a look at the constructor of PrintWriter and see how it is overloaded.
I solve this problem using a home grown TeeWriter class which decorates two or more Writers and writes everything to them all. I have similar classes TeePrintWriter and TeeOutputStream. Very easy to write.
Retired horse trader.
Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Rob Spoor wrote:TeePrintWriter (or TeePrintStream, in case you have it as well) shouldn't be necessary. Just wrap a TeeWriter / TeeOutputStream in a new PrintWriter (or PrintStream).
Indeed. I wrote these classes about a decade ago such that they all took as arguments two streams of the same basic type. With the introduction of varags I converted the TeeOutputStream so that it now takes a varags list of OutputStreams but I didn't bother updating the other Tee streams as I rarely use them.
One of the advantages of using the varags TeeOutputStream is that it immediately got rid of my need for an OutputStream to discard all input (I use that in some JUnit tests when checking for exceptions). I just have to give it nothing to chain to.