This is my test code
long fileSize = 1024 * 1024 * 512L;
byte[] bts = new byte[8];
RandomAccessFile randomAccessFile = new RandomAccessFile("f:/test.data", "rw");
randomAccessFile.setLength(fileSize);
randomAccessFile.seek(0);
long time = System.nanoTime();
randomAccessFile.write(bts);
System.out.println("write1 use:" + (System.nanoTime() - time));
randomAccessFile.seek(1024 * 1024 * 256L);
time = System.nanoTime();
randomAccessFile.write(bts);
System.out.println("write2 use:" + (System.nanoTime() - time));
write1 use:181051
write2 use:2029338072
It can be seen that writing is 9 bytes twice and the second time is 10000 times slower than the first time.
So I would like to ask why the seek will cause the file to write so slowly. Is there any solution?