This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Why don't you create a method that writes to a file and then call it twice with different parameters?
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
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.
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).
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.