bachurim09
bachurim09

Reputation: 1811

RandomAccessFile Java - complexity

i am using RandomAccessFile in a Java program on Linux that deals with a huge amount of data.

so what i am doing is i keep many files, each file contains different information.

when i preform the action

int x=???//some large number
RandomAccessFile rand = new RandomAccessFile("file.txt","r");
rand.seek(x); //the file contains more than x bytes
 byte b = rand.readByte();

what is the complexity of the program? does the program preform 2 actions in the last 2 lines? one for seeking to the xth byte and one for reading the byte? - in other words the whole file is in one consecutive location on the disk(like an array)? or does it preform x actions for the seek and one for the reading?

thank you

Matt

Upvotes: 4

Views: 1161

Answers (3)

trutheality
trutheality

Reputation: 23465

seek is almost constant. Positioning the pointer to the xth byte is usually O(1) like an array lookup, but sometimes files are fragmented on disk, it can take #-of-fragments steps to find the xth byte.

Upvotes: 0

Marko Topolnik
Marko Topolnik

Reputation: 200158

Seek is O(1) or close to that. It doesn't have to run through the file to your position.

Upvotes: 4

gulyan
gulyan

Reputation: 662

Seek just positions the internal pointer, it doesn't read anything from the disk.

Upvotes: 3

Related Questions