Hello Sohail,
First your code will not compile. As RandomAccessFile constructor, seek() and close() method in your program throw IOException. You have to either catch them in try catch block ot declare in throws clause of main() method.
On the Assumtion that code will compile correctly, explanation given by Priya is correct. I would like to little more for your clarification:
After a random-access file is constructed, you can seek to any byte position within the file and then read or write. Pre-Java system (the C standard I/O library, for example) have supported seeking to a position relative to the beginning of the file, the end of the file, or the current position within the file.
Java's random-access files only support seeking relative to the beginning of the file, but there are methods that report the current position and the length of the file, so you can effectively perform the other kinds of seek as long as you are willing to do the arithmetic.
void seek(long position) throws IOException:
This sets the current position within the file, in bytes. Subsequent reading and writing will take place starting at this position. Files start at position 0.
Priya,
I dont agree with you on this point:
Try position at 9 through seek. Long will be printed.
file.seek(9);
System.out.println(file.readInt());
It will give runtime error as your trying to read long value with
readInt() method. The code will have to be modified to:
file.seek(9);
System.out.println(file.readLong());
------------------
Regards,
Raj.
-------------------------
Afforts should be Appriciated.
-------------------------