• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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: 22803
131
Eclipse IDE Spring 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.
 
Skool. Stay in. Smartness. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic