• Post Reply Bookmark Topic Watch Topic
  • New Topic

FileWriter and BufferedWriter  RSS feed

 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to check about my understanding about these two classes.
Please correct me if I am wrong somewhere.....


Each time we invoke write() method of FileWriter class then CPU gets busy in searching for the file and then write data to the file.

But when we use BufferedWriter write() method , then CPU writes data in the buffer and when this buffer space is finished it is flushed to the file.

Am I correct ?

What all resources we are saving by using BufferedWriter instead of FileWriter?
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always thought the idea of buffering readers and writers was so they would read or write a whole line at a time, so you are getting better performance.

Most of the time, if writing a text file, I don't use writers any more, but a java.util.Formatter.
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not unless you do a flush or close on the writer

Check this program..

Unless you use a sufficiently large string nothing will be written to the file and the program quitely terminates.

So there is some buffering going on in the writer as well?


[ August 26, 2008: Message edited by: Gamini Sirisena ]
 
Prashanti Mukund
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when we use FileWriter write() method is used cpu writes only one character at a time .so if there are 10 characters to be written in a file cpu need to make 10 i/o cycles and that is a performance issue.when we use buffered writer class's write method data stored in buffer and when we flush data gets stored in form of chunks by which the i/o cycles cpu has to make are reduced
 
Ananth Chellathurai
Ranch Hand
Posts: 349
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prashanthi Welcome to Java Ranch.

Can you please tell me where do you find this information. An URL will be useful.

Ananth Chellathurai
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!