File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes How to read a particular record? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "How to read a particular record?" Watch "How to read a particular record?" New topic

How to read a particular record?

Karthik Balasubramanian
Ranch Hand

Joined: Dec 13, 2002
Posts: 43
I just started working on Bodgitt & Scarper assignment. I wrote a method to read the DB file. It is working fine. But when I read the requirements of the assignment, it says:
"Your data access class must be called "", must be in a package called "suncertify.db", and must implement the following interface:
package suncertify.db;
public interface DB
// Reads a record from the file. Returns an array where each
// element is a record value.
public String[] read(int recNo) throws RecordNotFoundException;"
From above, I understood that "public String[] read(int recNo)" method should return the particular record based on the parameter we pass. That is, if we pass 3, it should return just the 3rd record..Is my understanding correct?
If my understanding is correct, can you guys suggest me how to get this done? Because, the db records does not have any index...I dont know how to do this...please help me out on this.
Jay Bromley
Ranch Hand

Joined: Aug 09, 2003
Posts: 48
Your understanding of the assignment specification is correct.
You are correct in saying that there are no _explicit_ record indices in the file, but there certainly is an implied record number. The first record would be 0, the second record would be 1, and so on. The header of the database file contains information about the byte offset of the first record, and the lengths of all the data fields, which lets you figure out the length of a record, so putting these two together with a record index number you can calculate the starting offset of any record.
[ December 12, 2003: Message edited by: Jay Bromley ]
Bill Robertson
Ranch Hand

Joined: Mar 21, 2003
Posts: 234
think of it in from a math standpoint.
if the header size is 40, and each record is 80, then you know record
1 must start at 40 + (80 * 1). Record 3 must start at 40 + (80 * 3)
and record X must start at 40 + (80 * X).....
Karthik Balasubramanian
Ranch Hand

Joined: Dec 13, 2002
Posts: 43
Thanks Bill and Jay. It was very helpful.

I agree. Here's the link:
subject: How to read a particular record?
It's not a secret anymore!