Reputation: 1811
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
Reputation: 23465
seek
is almost constant. Positioning the pointer to the x
th 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 x
th byte.
Upvotes: 0
Reputation: 200158
Seek is O(1) or close to that. It doesn't have to run through the file to your position.
Upvotes: 4
Reputation: 662
Seek just positions the internal pointer, it doesn't read anything from the disk.
Upvotes: 3