As per java docs,
In general, a Writer sends its output immediately to the underlying character or byte stream. Unless prompt output is required, it is advisable to wrap a BufferedWriter around any Writer whose write() operations may be costly, such as FileWriters and OutputStreamWriters. For example,
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
will buffer the PrintWriter's output to the file. Without buffering, each invocation of a print() method would cause characters to be converted into bytes that would then be written immediately to the file, which can be very inefficient.
But the following code appears as though FileWriter also buffers bytes before flushing to the output stream.
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class TestFileWriter {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
FileWriter fileWriter= new FileWriter("test.txt");
File file=new File("test.txt");
for (int i=0; i<1000; i++) {
fileWriter.write("a very long string, a very long string, a very long string, a very long string, a very long string\n");
if(i%50 == 0)
System.out.print("file size=" + file.length()+",");
}
fileWriter.close();
System.out.println("file size=" + file.length());
}
}
Output :-
file size=0,file size=0,file size=8192,file size=8192,file size=16384,file size=24576,file size=24576,file size=32768,file size=32768,file size=40960,file size=49152,file size=49152,file size=57344,file size=57344,file size=65536,file size=73728,file size=73728,file size=81920,file size=81920,file size=90112,file size=99000
Why isn't the data being written to the file as and when it is being written ?