File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructing a PrintWriter object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "constructing a PrintWriter object" Watch "constructing a PrintWriter object" New topic

constructing a PrintWriter object

Eric Gonzalez

Joined: May 01, 2002
Posts: 10
What are the difference between this to constructing ways? Which one I should use?
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(outputfile)));
PrintWriter pw = new PrintWriter(new FileOutputStream(outputfile), true);
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You should use whichever is appropriate for your application.
Actually, the first will probably solve most of your needs. You get buffering and use Writers which is usually a good idea.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
Hi, Eric
The main difference in constructor declaration is:
First declation calls
public PrintWriter(OutputStream out)constructor,
and another calls
PrintWriter(OutputStream out,boolean autoFlush ) constructor with autoFlush=true parameter.
And the difference is in autoFlush parameter value. (In first declaration it's false).
Jamal Hasanov
I agree. Here's the link:
subject: constructing a PrintWriter object
It's not a secret anymore!