learn java
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes The file format is wrong. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "The file format is wrong." Watch "The file format is wrong." New topic

The file format is wrong.

Wilder C Rodrigues
Ranch Hand

Joined: May 03, 2003
Posts: 107
I discovered that my .db file has a wrong format. So, for that I couldn't execute build my application.
The format of data in the database file is as follows:
Start of file
4 byte numeric, magic cookie value. Identifies this as a data file
4 byte numeric, total overall length in bytes of each record
2 byte numeric, number of fields in each record
Schema description section.
Repeated for each field in a record:
2 byte numeric, length in bytes of field name
n bytes (defined by previous entry), field name
2 byte numeric, field length in bytes
end of repeating block
But, in the start of file there is another number that was not posted by Sun. I've send an e-mail to sun aking for another assignment.
Now, I know the problem and I can read some fields of the file. But it's still having me trobles. I'm waiting for Sun response.
I made an application that read the file. I just don't know: what does the fourth number in the start of file do?
Wilder C. Rodrigues

SCEA Part I, SCAJ, SCPJ, SCDJ, SCWCD, SCBCD, SCMAD<br /> <br />"The significant problems we face can not be solved at the same level of thinking we were at when we created them." - Albert Einstein
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17260

The fourth number is the number of fields in a record.

Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Wilder C Rodrigues
Ranch Hand

Joined: May 03, 2003
Posts: 107
So, I know that fourth number is the number of the field in each record. Nut, what is the third number?
Wilder C. Rodrigues
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17260

What I mean is the first number is the magic number, that tells the program that it is a valid .db file.
The second number is how many bytes each record is.
The third number is the number of fields in a record.
a Record is
Name 20 chars
Address1 25 chars
Address2 25 chars
City 15 chars
State 2 chars
Zip 10 chars
number of bytes each record 97
number of fields is 6
Is that what you are asking?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
It would help if you can dump the header portion (if you have Linux you can use the "od" command, windows dunno) and let us know what it really contains.
In my NX db-2x2.db (Contractors) file I have the "offset to the first data record" in the header portion.
[ May 10, 2003: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Wilder C Rodrigues
Ranch Hand

Joined: May 03, 2003
Posts: 107
In my case, the file db-2x1.db (Contractors) has more than the Sun said.
But you are rigth when you said:
"The first number is the magic number.
The second number is how many bytes each record is."
But the third number is not the number of fields in a record. And I don't know what is that. I can read the first and the second, but I just read the number of the fields in the NINETH position. There is many datas between fourth and nineth position. I got read the file because before I looked inside it and could see what I was looking for.
I've sent an e-mail for Sun asking for help, but still now, nothing.
My database schema:
The database that Bodgitt and Scarper uses contains the following fields: Field descriptive name Database field name Field length Detailed description
Subcontractor Name name 32 The name of the subcontractor this record relates to.
City location 64 The locality in which this contractor works
Types of work performed specialties 64 Comma separated list of types of work this contractor can perform.
Number of staff in organization size 6 The number of workers available when this record is booked
Hourly charge rate 8 Charge per hour for the subcontractor. This field includes the currency symbol
Customer holding this record owner 8 The id value (an 8 digit number) of the customer who has booked this. Note that for this application, you should assume that customers and CSRs know their customer ids. The system you are writing does not interact with these numbers, rather it simply records them. If this field is all blanks, the record is available for sale.
So, length in bytes is 182 and number of field is 6. I got it this way:
FIELDS_NUM = data.raf.readByte();
System.out.println("Length of bytes in each record: "+data.HEADER_LEN);

System.out.println("Number of fields: "+FIELDS_NUM);
System.out.println("Fields name and length:");

for(int i = 0; i < FIELDS_NUM; i++){
System.out.println(" "+data.raf.readByte());
But I think that is not the rigth way.
What you can say about that? I have to wait for Sun's response?
Wilder C. Rodrigues
SCPJ2 1.4
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17260

Well in my beta version of the assignment, we had to create the db.db file from scratch.
Here is some code from my MakeData class, maybe this is close maybe not, maybe this might shed a light on your issue, maybe not.

OK, so I am now going to post the variable declarations so you know what my constant values are for the above code

I hope this helps, otherwise you will have to wait till Sun gets back with you.
S. Ganapathy
Ranch Hand

Joined: Mar 26, 2003
Posts: 194
Hi Wilder Rodrigues,
I am also using the same db file as you are using. I am not facing any problem. So you better look at your code while reading the data file. As per the db specification in the instructions, it is working fine.
You are using data.raf.seek(9);
instead, use data.raf.seek(8); it will surely work. You are making mistake there.
Shank Boston

Joined: Mar 29, 2005
Posts: 3
I am getting number of " total overall length in bytes of each record " to be 0. I did a raf.seek(5) after reading the magic cookie! Any idea what is wrong. I have the same db-2x1.db assingment...
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11627

Hi Shank,

I think you are seeking to the wrong location - in the same way that Java arrays start with index 0, the file offset starts at index 0. So by seeking to byte 5, you are effectively skipping the first 5 bytes. Try setting your seek value to 4 and see if you then get the desired results.

(You shouldn't need to seek in this case anyway, since having just read the Magic Cookie, your file pointer offset should be in the right location to read the next value).

Regards, Andrew

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
It is sorta covered in the JavaRanch Style Guide.
subject: The file format is wrong.