File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Writting into two files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Writting into two files" Watch "Writting into two files" New topic
Author

Writting into two files

Romeo Ranjan
Ranch Hand

Joined: Feb 23, 2009
Posts: 95
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


SCJP 6.0
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

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.


Mohamed Sanaulla | My Blog
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

Why are you not using BufferedWriter?


Java Experience
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Sunny Bhandari wrote:Why are you not using BufferedWriter?


PrintWriter does provide lot more methods.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

The IO classes where designed to be able to be chained. That way you can combine them:
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

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
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

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).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writting into two files
 
Similar Threads
java.sql.SQLException: Exhausted Resultset
About PrintWriter with println and write
deleting a line from a text file
Problem with EDIT button.
PrintWriter please help