| 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.
|
 |
 |
|
|
subject: Information: PrintWriter & FileWriter
|
|
|