aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes NX:[URLyBird]Get the correct field values 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 "NX:[URLyBird]Get the correct field values" Watch "NX:[URLyBird]Get the correct field values" New topic
Author

NX:[URLyBird]Get the correct field values

Richard Jackson
Ranch Hand

Joined: Jun 25, 2003
Posts: 128
Dear friends in this forum,
In my assignment,there are some sentences describing as follows
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
2 byte numeric, number of fields in each record
Schema description section.
Repeated for each field in a record:
1 byte numeric, length in bytes of field name
n bytes (defined by previous entry), field name
1 byte numeric, field length in bytes
end of repeating block
Data section.
Repeat to end of file:
1 byte flag. 00 implies valid record, 0xFF 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.

I have gotten the value of magicCookie and numberOfFields.But inSchema description section,I can't continue.
Now given code is my done.

n bytes (defined by previous entry), field name

If I can't use readUTF() method,please tell me the way I can get the "field name" value.
Regards,
Richard


Regards, Richard
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11460
    
  94

Hi Richard,
You now know the number of bytes in the field name (fieldnameLength), so you just have to read that many bytes into an array of bytes. Then convert them into a String.
When you read the bytes into the byte array, you probably want to ensure that you do read fully all the bytes that form the name (as opposed to potentially only reading some of the bytes.
If you look at the API for DataInputStream you should find a method that matches the hints that I am providing.
Note: when you convert from the array of bytes into a String, you probably want to ensure that the String is created using the apropriate charSet to meet the requirements of your instructions.
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Richard Jackson
Ranch Hand

Joined: Jun 25, 2003
Posts: 128
Hi,Andrew.
Thank you for your rapid reply.
so you just have to read that many bytes into an array of bytes. Then convert them into a String.

Yes,I try do it.And the code like this

According to your another statement
When you read the bytes into the byte array, you probably want to ensure that you do read fully all the bytes that form the name (as opposed to potentially only reading some of the bytes.

I find the readFully(Byte[] b) method, but I dont know how to use.
Because its return value type isvoid.
Wait for your note.
Regards,
Richard
Chris Harris
Ranch Hand

Joined: Sep 21, 2003
Posts: 231
Hi Richard,
If I undersand your question right you are trying to read the names from the DB. You may want to try some thing like:


Chris


SCJP 1.2, SCWCD, SCBCD
Richard Jackson
Ranch Hand

Joined: Jun 25, 2003
Posts: 128
Hi,Chris. Thanks for your reply.
I test my code again after plusing your code piece,but I can't compile completely.
can't resolve symbol

So I looked up relational API in JDK 1.4.1 documents.According to this,I try it again as follow,

But when I run the program,it still say some error in method String
andcan't resolve symbol
I am really confused.
Regards,
Richard
Davidd Smith
Ranch Hand

Joined: Jul 28, 2003
Posts: 62
Hi,
Richard,

fieldName=String(name,"UTF-8");

It should be fieldName = new String(name, "US-ASCII);
It uses a constructor of String, you could consult the API. "UTF-8" is not "US-ASCII" though it could still get correct result from your db.db file.
Regards
Davidd
[ September 23, 2003: Message edited by: Davidd Smith ]
[ September 23, 2003: Message edited by: Davidd Smith ]
Richard Jackson
Ranch Hand

Joined: Jun 25, 2003
Posts: 128
Hi,Davidd.Thank you.
I do exactly make mistake when I enter the code.
And the piece of code can be compiled.As you said,
"UTF-8" is not "US-ASCII" though it could still get correct result from your db.db file.

But there is still a sentence
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.

The8 bit US ASCII means what?
Does it imply that I must use" new String(name,"UTF-8") " ?
Regards,
Richard
Richard Jackson
Ranch Hand

Joined: Jun 25, 2003
Posts: 128
Hi,all
I have done the test application.Part of code like this,

Thus,I have gotten length of field name/field name/field length.
1)How can I work out header length and record count/length?
2)Next step,how can I continue to get each record?
Please help me if possible.
Thanks in advance.
Regards,
Richard
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11460
    
  94

Hi Richard,
How can I work out header length

Keep track of how many bytes you have read.
How can I work out [...] record [...] length?

Add together all the field lengths and then add the number of bytes used to indicate whether the record is deleted or not (refer to your instructions)
How can I work out [...] record count?

( Size of file - header length ) / record length
Note that this will not indicate whether the records are available or not - you will have to look at the deleted flag to determine that.
Next step,how can I continue to get each record?

You now know where the data starts, and how big it is. Proceed with that.
Regards, Andrew
Richard Jackson
Ranch Hand

Joined: Jun 25, 2003
Posts: 128
Hi,Andrew
I do the test program again and again.And I write code as follows,

I think that I have already read the file header and schema info.But I dont ensure that's true or not.Please correct that for me.
In order to finish Data class well,I should read all the records step by step.
Have you had some ways to solve that?
You now know where the data starts, and how big it is.

Also I dont know the place where data starts...
Regards,
Richard
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11460
    
  94

Hi Richard,
I think that I have already read the file header and schema info.But I dont ensure that's true or not.

The header section is the bit that you have read in that contains the magic cookie and the number of fields in the schema.
The schema is the section you have read in which contains the size of the column name, the column name, and the size of the column.
So you have read both.
Also I dont know the place where data starts...

According to the instructions, the data section starts immediately after the schema.
In order to finish Data class well,I should read all the records step by step.
Have you had some ways to solve that?

As you read each column name and column size in the schema, store them in a collection.
Then when you get to the data section read the delete status flag, then for each item in your collection, read the appropriate number of bytes and convert them into a String for the value for that column. Repeat until end of file is reached (or until you have read the calculated number of records).
Regards, Andrew
Richard Jackson
Ranch Hand

Joined: Jun 25, 2003
Posts: 128
Hi,Andrew
Thanks for your help and advice.
1)I'll try again to implement read records.
2)I've already closed another same topic "read all records".
Regards,
Richard
Richard Jackson
Ranch Hand

Joined: Jun 25, 2003
Posts: 128
Hi,Andrew
I select this topic to remain here.I'm sorry to do that.
Thanks.
Regards,
Richard
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NX:[URLyBird]Get the correct field values