As per the API, these are the facts:
- The
seek(long bytePosition)
method simply put, moves the pointer to the position specified with thebytePosition
parameter. - When the
bytePosition
is greater than the file length, the file length does not change unless a byte is written at the (new) end. - If data is present in the length skipped over, such data is left untouched.
However, the situation I'm curious about is: When there is a file with no data (0 bytes) and I execute the following code:
file.seek(100000-1);
file.write(0);
All the 100,000 bytes are filled with 0
almost instantly. I can clock over 200GB in say, 10 ms.
But when I try to write 100000 bytes using other methods such as BufferedOutputStream
the same process takes an almost infinitely longer time.
What is the reason for this difference in time? Is there a more efficient way to create a file of n
bytes and fill it with 0
s?
EDIT: If the data is not actually written, how is the file filled with data? Sample this code:
RandomAccessFile out=new RandomAccessFile("D:/out","rw");
out.seek(100000-1);
out.write(0);
out.close();
This is the output:
Plus, If the file is huge enough I can no longer write to the disk due to lack of space.