why are java RandomAccessFile so much slower than FileOutputStream?
<p>As long as I can understand java api, opening a RandomAccessFile using "rw" does not write ervery single byte synchronously on the underlying storage device. Unlike with "rws" or "rwd".<br>
Why is it almost the same "speed" like the unbuffered FileOutputStream with "rw" and more than 10 times slower with "rws"/"rwd"?</p>

<p>the following simple code shows this, and I cannot get any reasonnable explanation to this. Any clue?</p>

<pre><code>import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;

public class StreamTest {

  public static void main(String[] args) throws Exception {

    OutputStream os;
    RandomAccessFile raf;
    int size = 10000;
    File file = new File("test.log");

    long a=System.currentTimeMillis();
    os = new FileOutputStream(file);
    for(int i=0;i&lt;size;i++){
      os.write(("1").getBytes());
    }
    os.close();     
    long b=System.currentTimeMillis();
    System.out.println("writing direct "+(b-a));

    raf = new RandomAccessFile(file,"rws");
    for(int i=0;i&lt;size;i++){            
      raf.write(("1").getBytes());
    }
    raf.close();

    long c=System.currentTimeMillis();
    System.out.println("random access write "+(c-b));

    raf = new RandomAccessFile(file,"rw");
    for(int i=0;i&lt;size;i++){            
      raf.write(("1").getBytes());
    }
    raf.close();

    long d=System.currentTimeMillis();
    System.out.println("random access optimized write "+(d-c));
  }
}
</code></pre>
