File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Information: PrintWriter & FileWriter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Information: PrintWriter & FileWriter" Watch "Information: PrintWriter & FileWriter" New topic
Author

Information: PrintWriter & FileWriter

Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
FileWriter is the character representation of IO. That means it can be used to write characters. Internally FileWriter would use the default character set of the underlying OS and convert the characters to bytes and write it to the disk.
PrintWriter & FileWriter.
Similarities
a) Both extend from Writer.
b) Both are character representation classes, that means they work with characters and convert them to bytes using default charset.
Differences
a) FileWriter throws IOException in case of any IO failure, this is a checked exception.
b) None of the PrintWriter methods throws IOException , instead they set a boolean flag which can be obtained using checkError().
c) PrintWriter invokes flush after every byte of data is written , automatically. In case of FileWriter, invoker as to take care of invoking flush.

Difference between PrintStream and OutputStream: Similar to above explanation, just replace character with byte.

In case you guys know more difference , post it. Further please correct me whereever possible,


SCJP, SCWCD, SCBCD
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Hi Deepak,
Thanks for the useful information

Preetha
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
nice info Deepak:

PrintWriter also provides:

append()
format()
print()
printf()
println()

methods.
[ December 08, 2008: Message edited by: Punit Singh ]

SCJP 6
Amol Nakhwa.
Greenhorn

Joined: Jan 25, 2010
Posts: 2
PrintWriter has following methods :
close()
flush()
format()
printf()
print()
println()
write()

and constructors are :
File (as of Java 5)
String (as of Java 5)
OutputStream
Writer

while FileWriter having following methods :
close()
flush()
write()

and constructors are :
File
String

//ref. SCJP5
Jeevan Reddy
Ranch Hand

Joined: Nov 10, 2009
Posts: 142
Thanks guys
It was informative


SCJP 1.6 (94%)
http://faq.javaranch.com/java/JspFaq
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

b) None of the PrintWriter methods throws IOException , instead they set a boolean flag which can be obtained using checkError().


True. I'd just like to add that though none of the methods of Printwriter throw IOException, a couple of constructers of PrintWriter may (the ones that take String and File objects as arguments).

Nice compilation, Deepak!


"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
Marcel Chasiguasin
Greenhorn

Joined: Jul 15, 2011
Posts: 12
i have a questio:

What is the difference between PrintWriter.write(String) and PrintWriter.print(String)?

Regards
Marcel.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Information: PrintWriter & FileWriter
 
Similar Threads
Need Help in parsing Japanese SHIFT JIS Characters in Java
IO ??????
Understanding Byte Data and Character Encoding
I/O Wrappers
what is the difference between printStream and PrintWriter?