• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Writting into two files

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you not using BufferedWriter?
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sunny Bhandari wrote:Why are you not using BufferedWriter?



PrintWriter does provide lot more methods.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The IO classes where designed to be able to be chained. That way you can combine them:
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
A timing clock, fuse wire, high explosives and a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic