• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Could anyone help on seek() please?

 
alzamabar
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, while waiting for my exam, I'm reading post here and there. I learnt from George most on what I'll need to write in order to read the db infos. There will be a file with metadata, headers and actual data, keeping to same formats and lenght it will be possible to read in a loop (at the end it's this that we want) the data, using RandomAccessFile is recommended. I'm encountering in numerous posts the RAF.seek() method. I would like to explain as my defence that I am not english-mother-language. I had a look a the documentation for this method and it sais:

Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs. The offset may be set beyond the end of the file. Setting the offset beyond the end of the file does not change the file length. The file length will change only by writing after the offset has been set beyond the end of the file.

I didn't understand a lot of it.
If I've got (in bytes):
1234 6789 ABC
If I perform RAF.seek(4), will the pointer be before '6'? Probably there will be something wrong in what I've just asked,, but anyway, what's the utility of using seek() (Please note this is not a critizism of the method, it's just that I would like to know how to get advantage of it for my SCJD exam!).
Thanks for any help,
Marco
[Andrew: Changed the [code] tags to [quote] tags so that horizontal scrolling is not required]
[ March 11, 2004: Message edited by: Andrew Monkhouse ]
 
George Marinkovich
Ranch Hand
Posts: 619
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marco,
Originally posted by Marco Tedone:
Hi, while waiting for my exam, I'm reading post here and there. I
learnt from George most on what I'll need to write in order to read the db
infos. There will be a file with metadata, headers and actual data,
keeping to same formats and lenght it will be possible to read in a loop
(at the end it's this that we want) the data, using RandomAccessFile is
recommended. I'm encountering in numerous posts the RAF.seek() method. I
would like to explain as my defence that I am not english-mother-language.
I had a look a the documentation for this method and it sais:

I didn't understand a lot of it.
If I've got (in bytes):
1234 6789 ABC
If I perform RAF.seek(4), will the pointer be before '6'? Probably there
will be something wrong in what I've just asked,, but anyway, what's the
utility of using seek() (Please note this is not a critizism of the
method, it's just that I would like to know how to get advantage of it for
my SCJD exam!).
Thanks for any help,
Marco

One way to access the records in the database file is as follows:
When the database file is opened the, read the header and the database
schema. Having read the header and schema you will be in a position to
know the following things: the record data offset (the position in the
database file at which the record data actually starts), and the size in
bytes of each record. Knowing this information allows you to seek to a
particular record number according to the following formula:
file.seek(dataOffset + (recNo * recordSize));
You can then read the record at this position in the file. Or do any of
the other database operations (update, delete) that take a record number
as a parameter.
 
Mark Smyth
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The seek() method positions the file pointer at the offset in bytes you want to read from or write to next.
file.seek(0) is the beginning of the file
Somewhere In the file header of the assignment you will have the total length in bytes of the header after which actual records begin.
so to go to the first record you would have
file.seek(HEADER_LENGTH);
And to find a particular record number would be something like
file.seek(HEADER_LENGTH+(RECORD_LENGTH*recordNumber))
In your actual program you will probably have some kind of functions to
calculate these offsets and to read the header info, rather than relying on static constants.
 
Mark Smyth
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As always George puts it so much more clearly
 
alzamabar
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.
Offset + (recLength * recNbr) is a damn good formula!
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marco,
George and Mark have done a wonderful job of explaining how to use the seek() function, but I don't think they answered your explicit question. So in case that is still in doubt...

If I've got (in bytes):
1234 6789 ABC
If I perform RAF.seek(4), will the pointer be before '6'?

That depends on whether the space is in your file, or just part of your formatting
If the space is in the file, then the file pointer will be just before the space.
If the space is not in the file, then the file pointer will be just after the space, or just before the '6'.
Let's try that again with a slightly longer example:

According to my mapping above, the charcter 'k' is in position 11 (and I am assuming that each character is only occupying a single byte).
As Mark said, doing a seek(0) will set the file pointer to the start of the file - just before the letter 'a'. The next character to be read will be 'a'.
Doing a seek(4) will always move the file pointer to four bytes into the file - to just before the 'e'.
Doing a skipBytes(2) at that point will move the file pointer to just before the 'g'.
Doing a seek(4) will return the file pointer to four bytes into the file - to just before the 'e'.
Regards, Andrew
[ March 11, 2004: Message edited by: Andrew Monkhouse ]
 
alzamabar
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrew, you've written:

'k' is at position 10, thus confirming to me that the file positions start at 0.
So in the case where the space is in the file:
1234 6789
seek(4) will position the pointer before 6. Is that correct?
Marco
[ March 12, 2004: Message edited by: Marco Tedone ]
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marco,
Sorry, my proofreading skills were obviously non existant last night.
For a start, I should have said that 'k' was in position 11 according to my diagram (where I was starting counting from 1). But you are correct - if you are counting from zero, then it is in position 10.
Additionally, I meant that there was a difference if you were counting the space in your file. That is, if your file contained:

Where the space is in the file, then seek(4) will move the file pointer to just before the space.
If you are not counting the space, that is, if your file contained:

Then seek(4) will move the file pointer to just before the '6'.
Regards, Andrew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic