yes i know...it is not the part of threads...it is of the io streams...... i dont knw...but even after reading about seek method...i coudnt get what it is actually....... can you please explain me with a certain example or something

Tony Docherty

Bartender

Posts: 3264

81

posted 4 years ago

2

I'm not sure I can explain it any better than the API docs but here goes;

A RandomAccessFile treats a file on disk like a large byte array. You can access any byte in the file by specifying an offset from the beginning of the file in the same way you can access any element in an array by specifying a 0 based index. In a RandomAccessFile, the method you use to specify this offset is seek(). When you call seek() with a value it positions the file pointer to that location and subsequent reads and/or writes happen relative to the current file pointer location. Note, reads and writes will advance the file pointer to the next location after the bytes read/written. So if you want to read the 10th byte in the file call seek(10) and then call read().

Midhat Mukadam wrote:yes i know...it is not the part of threads...it is of the io streams...... i dont knw...but even after reading about seek method...i coudnt get what it is actually....... can you please explain me with a certain example or something

No need for an example, the concept is pretty easy. Basically, the way to locate data on a file is via an index. The beginning of the file is location zero. Ten bytes in is location ten. Two kilobytes from the beginning is 2 times 1K, or 2 times 1024 bytes, or location 2048. Etc. Etc. Etc.

When you first open a file, it is at location zero, and as you read the file, the location index increments. However, what if you don't want to read from the beginning? What if you want to move around the file? To do that you seek to the particular location via the seek() method.

And next time, please be more careful. If you know that it is not threads related, please don't put it in the threads forum -- please put it in the correct forum.