This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Reading the binary db file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Reading the binary db file" Watch "Reading the binary db file" New topic
Author

Reading the binary db file

GD Deepz
Ranch Hand

Joined: Sep 29, 2004
Posts: 55
Just got my assignment, the db file for the B&S. I plan to create an object for each record, i.e an object for contractor and one for customer but how do I read the db binary file using RAF and "extract" the contractor name and customer name.
Andy Zhu
Ranch Hand

Joined: May 26, 2004
Posts: 145
I used RandomAccessFile to read the db file. Read the api, it's quite easy. I think some other use nio but I am not familiar with that. Max' book have a topic over it.


--------<br />Andy Zhu<br />scjp 1.4<br />scjd 1.4<br />SAS Certified Programmer 9.0
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
You can't use NIO for the exam.

See SCJD Exam Requirements at Sun.


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by Jeff Bosch:
You can't use NIO for the exam.

See SCJD Exam Requirements at Sun.


You may be able to use NIO, if your specific instructions don't ban it, but you don't really need it for this assignment. A RandomAccessFile is probably the easiest solution.

And absolutly don't use non-blocking NIO for the network, the spec says Sockets or RMI. Non-blocking SocketChannels and Selectors are still full of pitfalls in JDK1.4, they appear to work well in 1.5 though. Non-blocking SocketChannels also don't support ObjectInputStreams.
GD Deepz
Ranch Hand

Joined: Sep 29, 2004
Posts: 55
Funny thing in my B&S, there is no "owner" in the db file provided by Sun, I guess I have to allocate a new owner to be provided by the user. Any feedback? Searching for a paticular string or int is not easy with RAF. Suggestions?
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11437
    
  85

Hi GD,

Welcome to JavaRanch and this forum.

Check your instructions - there should be an "owner" field. Mine has:



Your instructions may be different, but I would be surprised if they did not have something that is not similar to this in some respect.

Searching for a paticular string or int is not easy with RAF. Suggestions?


Check your instructions - possibly the methods of the Data class all require you to work with String or String[]. So all you need to do is convert from the format you read from file into String or String[].

Regards, Andrew


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

Joined: Sep 29, 2004
Posts: 55
Hi Andrew, tks for your reply. There is an owner "field" but no metadata for the owner field, meaning there no owners have been allocated for each contractor. Sun must have left this out and How do I read each String using raf? Is there a formula? I tried some formulas but none worked.
Mike Vess
Ranch Hand

Joined: Aug 25, 2004
Posts: 41
Try this to read a string from a RAF:

byte[] data = new byte[fieldLen];
raf.read(data);
String value = new String(data);


What you thought was right today may need a refactoring tomorrow...
Anton Golovin
Ranch Hand

Joined: Jul 02, 2004
Posts: 476
Originally posted by Mike Vess:
Try this to read a string from a RAF:

byte[] data = new byte[fieldLen];
raf.read(data);
String value = new String(data);


I used new String(data, "US-ASCII"), due to the fact that the String constructor does not properly convert bytes to the correct charset in some circumstances.
[ October 03, 2004: Message edited by: Anton Golovin ]

Anton Golovin (anton.golovin@gmail.com) SCJP, SCJD, SCBCD, SCWCD, OCEJWSD, SCEA/OCMJEA [JEE certs from Sun/Oracle]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11437
    
  85

Hi GD,

There is an owner "field" but no metadata for the owner field, meaning there no owners have been allocated for each contractor.


That is correct - as supplied, none of the contractors (or hotels) have been booked.

When the CSR runs your client software, they will choose to book one of the contractors, and at that time they will enter the customer number.

You will then need to persist this number into the data file.

Regards, Andrew
GD Deepz
Ranch Hand

Joined: Sep 29, 2004
Posts: 55
Doing the following is not a problem:

byte[] data = new byte[fieldLen];
raf.read(data);
String value = new String(data);

but the whole string, I mean the entire db file is read as "one" string into "value". Now I need to parse this string to extract Contractor, location and etc. Parsing this ugly string to extract what I need is a huge stumbling block
Sean Gildea
Ranch Hand

Joined: Jul 01, 2004
Posts: 81
but the whole string, I mean the entire db file is read as "one" string into "value". Now I need to parse this string to extract Contractor, location and etc. Parsing this ugly string to extract what I need is a huge stumbling block


I am at the same problem.

My question is:

Do I use



to get the first 70 chars for example? and then repeat that process for the rest of the record?
[ October 22, 2004: Message edited by: Sean Gildea ]

SCJD, SCBCD, SCJP
Matt Sheehan.
Ranch Hand

Joined: Oct 08, 2004
Posts: 63
I think you'd be in better shape if you kept the fields separate from the start. Like this:

Now recordValues has all the field values for this record and they are in a format returnable by the DB interfaces read method.

Matt
cooper davise
Greenhorn

Joined: Apr 15, 2013
Posts: 1
Doing the following is not a problem:

byte[] data = new byte[fieldLen];
raf.read(data);
find binary file info by http://binarydb.com.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading the binary db file