• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading the binary db file

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't use NIO for the exam.

See SCJD Exam Requirements at Sun.
 
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
GD Deepz
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this to read a string from a RAF:

byte[] data = new byte[fieldLen];
raf.read(data);
String value = new String(data);
 
Ranch Hand
Posts: 531
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doing the following is not a problem:

byte[] data = new byte[fieldLen];
raf.read(data);
find binary file info by http://binarydb.com.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic