| Author |
seek()
|
Raghav Mathur
Ranch Hand
Joined: Jan 12, 2001
Posts: 639
|
|
FileOutputStream fos = new FileOutputStream("xx"); 2. for (byte b=10; b<50; b++) 3. fos.write(b); 4. fos.close(); 5. RandomAccessFile raf = new RandomAccessFile("xx", "r"); 6. raf.seek(10); 7. int i = raf.read(); 8. raf.close() 9. System.out.println("i = " + i); why the output is 20 ?
|
Raghav.
|
 |
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
|
|
40 bytes (10 to 49) are written to the file named "xx". Then, when we open the file using a RandomAccessFile, we position the file pointer on the 10th byte which is 20. Content of the file after the writing: byte 0: 10 byte 1: 11 byte 2: 12 ... byte 9: 19 byte 10: 20 byte 11: 21 ...
|
SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
|
 |
Raghav Mathur
Ranch Hand
Joined: Jan 12, 2001
Posts: 639
|
|
but is it not that void seek ( long ) means seeking from a desired position . In this case , 10 to 50 , ie almost 40 bytes have been writin to the file . now when seek(10) is executed and it starts reading the content. Does it not mean that seeking from the 10th position till end of file , ,ie is 50 . so we actually start reding from the 20th byte till the 50th . still confused . please explain thanks in advance . raghav ...
Originally posted by Valentin Crettaz: 40 bytes (10 to 49) are written to the file named "xx". Then, when we open the file using a RandomAccessFile, we position the file pointer on the 10th byte which is 20. Content of the file after the writing: byte 0: 10 byte 1: 11 byte 2: 12 ... byte 9: 19 byte 10: 20 byte 11: 21 ...
|
 |
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
|
|
Raghav, I'm confused by your question. I'm not really sure what you mean, but if you check out the description for seek in the API, you'll see this:
Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.
Therefore, the file pointer is poitioned 10 bytes from the beginning of the file. Next, we call read, which reads a single byte from that data file. The next byte (from the current position of the file pointer) is the value 20. That's what is read in. I hope that helps, Corey
|
SCJP Tipline, etc.
|
 |
 |
|
|
subject: seek()
|
|
|