• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Information: PrintWriter & FileWriter

 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Deepak,
Thanks for the useful information

Preetha
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nice info Deepak:

PrintWriter also provides:

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

methods.
[ December 08, 2008: Message edited by: Punit Singh ]
 
Amol Nakhwa.
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys
It was informative
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Marcel Chasiguasin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a questio:

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

Regards
Marcel.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic