• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writting into two files

 
Romeo Ranjan
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

When I want to write contents to two different files this is the way I do it.



Intialize two FileWriter and two PrintWriter instances, is this a better practise ?

Thanks
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you create a method that writes to a file and then call it twice with different parameters?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sunny Bhandari
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you not using BufferedWriter?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sunny Bhandari wrote:Why are you not using BufferedWriter?


PrintWriter does provide lot more methods.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The IO classes where designed to be able to be chained. That way you can combine them:
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic