File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes question regarding to the data file format 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 "question regarding to the data file format " Watch "question regarding to the data file format " New topic
Author

question regarding to the data file format

Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
In the instruction.html, it says this:

Start of file
4 byte numeric, magic cookie value identifies this as a data file
4 byte numeric, offset to start of record zero
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

Data section. (offset into file equal to "offset to start of record zero" value)
Repeat to end of file:
2 byte flag. 00 implies valid record, 0x8000 implies deleted record
Record containing fields in order specified in schema section, no separators between fields, each field fixed length at maximum specified in schema information

End of file

All numeric values are stored in the header information use the formats of the DataInputStream and DataOutputStream classes. All text values, and all fields (which are text only), contain only 8 bit characters, null terminated if less than the maximum length for the field. The character encoding is 8 bit US ASCII.

In the db file, I get this:
ll F - L name location @ specialities @] size - ] rate | owner Dogs with Tools ....

Question:
Should I use RandomAccessFile to read the first 4 bytes, then read the second 4 bytes, read the next 2 bytes for the start of file?
Then, should I use RandomAccessFile to read 2 bytes, n bytes (what is n?), , then 2 bytes?

Then, should read the whole record ( 32+64+64+6+8+8 bytes) until the end of file?

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5201
    
  12

You can definitely use RandomAccessFile for reading/writing the database file. That's what I used too.

Then you have to choices: use a dynamic or static approach. With the static approach you use some constants indicating record length, start of data section of database file,... and you use these constants to read/write database file. Very simple and easy, but your Data class can only be used for a database file containing contractors. With the dynamic approach you read the database schema dynamically and you'll calculate the constants at run-time. This approach is a bit complexer (harder) but you can reuse your Data class to handle database files with contractors, customers,...

Both approaches are fine: I used the dynamic approach, Roberto the static one and we both passed with flying colours.

Good luck!

ps. if you have trouble with understanding your database file, you can use Roberto's database file reader (link provided in ScjdFaq)


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
I am having a hard time with this description because Monkhouse book does not have a header in their dvd.db. But anyway, here is what I think:
In a class, create byte [] header = new byte[10];
The first 4 bytes store an integer , magicCookieValue.
The second 4 bytes sotre an integer, totalLength
The next 2 byte store a short , numberOfFields (5 fields: name, location, size, rate, owner)
So, I end up with two integer and one short.

Now, I have 5 fields. For each field, I create a list since I don't know what n will be, I read 2 bytes , for field_name_length, n bytes (field_name_length, 64 bytes for name, 64 bytes for location or 8 bytes for customer ID) for field name, 2 bytes for field length.


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
If I write my Data class such that it checks the magic cookie for the first example, and expects the meta-data and schema for that first example, then it will be able to read any file that has the same magic cookie, meta-data and schema. But as you can easily see, without the check on the magic cookie, the Data class would quickly fail when trying to read the second file format - the field sizes are different, and some fields are different.

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5201
    
  12

That could work.

The best thing you can do to get familiar with your database file is to write a program which is able to read the whole content and output to the console in readible format. That's the 1st thing I did when I started the assignment.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question regarding to the data file format