• Post Reply Bookmark Topic Watch Topic
  • New Topic

BufferedWriter & PrintWriter ?  RSS feed

 
kelvin cheung
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what are the differences between them?
with and without using PrintWriter :



 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The biggest difference is that the print and println methods of PrintWriter take arguments of any type, generally calling the toString() or String.valueOf() methods to get String objects. The BufferedWriter write() method takes a single character, an array of characters, or a String.

println() always adds end of line characters to the end of the data.

Also, print() and println() never throw checked exceptions.

When writing to the screen, output takes place immediately. When writing to a disk file, output is buffered into blocks of "logical records" (do they still use that term?). If the program ends, without closing the file, data can be lost. You can call flush() after writing each line or you can use the two argument constructor - PrintWriter(file, autoflush) - and set autoflush to true.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the I/O and Streams forum...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!