Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java difference between FileWriter and BufferedWriter

What's the difference between those? I'm just learning Java ATM, but it seems like I can write to a file both ways i.e. (I didn't copy the try-catch block here.)

FileWriter file = new FileWriter("foo.txt"); file.write("foobar"); file.close(); 

and

FileWriter file = new FileWriter("foo.txt"); BufferedWriter bf = new BufferedWriter(file); bf.write("foobar"); bf.close(); 

I understand the concept of buffering the data first, so does that mean the first example writes the characters one by one and the second first buffers it to the memory and writes it once?

like image 358
Opi Avatar asked Sep 10 '12 10:09

Opi


People also ask

What is the main advantage of using BufferedWriter over FileWriter alone?

If you have a lot of code that appends a single character at a time, however, a BufferedWriter will be much more efficient. As per andrew's comment below, the FileWriter actually uses its own fixed-size 1024 byte buffer. This was confirmed by looking at the source code.

What is difference between BufferedWriter and PrintWriter?

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.

What is the difference between PrintWriter and FileWriter?

PrintWriter gives you some handy methods for formatting like println and printf . So if you need to write printed text - you can use it. FileWriter is more like "low-level" writer that gives you ability to write only strings and char arrays.

What is the difference between FileWriter and FileOutputStream?

FileWriter vs FileOutputStreamFileWriter writes streams of characters while FileOutputStream is meant for writing streams of raw bytes.


2 Answers

BufferedWriter is more efficient if you

  • have multiple writes between flush/close
  • the writes are small compared with the buffer size.

In your example, you have only one write, so the BufferedWriter just add overhead you don't need.

so does that mean the first example writes the characters one by one and the second first buffers it to the memory and writes it once

In both cases, the string is written at once.

If you use just FileWriter your write(String) calls

 public void write(String str, int off, int len)          // some code         str.getChars(off, (off + len), cbuf, 0);         write(cbuf, 0, len);  } 

This makes one system call, per call to write(String).


Where BufferedWriter improves efficiency is in multiple small writes.

for(int i = 0; i < 100; i++) {     writer.write("foorbar");     writer.write(NEW_LINE); } writer.close(); 

Without a BufferedWriter this could make 200 (2 * 100) system calls and writes to disk which is inefficient. With a BufferedWriter, these can all be buffered together and as the default buffer size is 8192 characters this become just 1 system call to write.

like image 103
Peter Lawrey Avatar answered Oct 01 '22 15:10

Peter Lawrey


You are right. Here is how write() method of BufferedWriter looks:

public void write(int c) throws IOException {     synchronized (lock) {         ensureOpen();         if (nextChar >= nChars)             flushBuffer();         cb[nextChar++] = (char) c;     } } 

As you can see it indeed checks whether the buffer is full (if (nextChar >= nChars)) and flushes the buffer. Then it adds new character to buffer (cb[nextChar++] = (char) c;).

like image 35
AlexR Avatar answered Oct 01 '22 17:10

AlexR