| Author |
NX: Reading from the db.db file
|
Murali Kurukunda
Ranch Hand
Joined: Oct 20, 2003
Posts: 36
|
|
Hi All, I have just started my assignment. It is Bodgitt and Scarper, LLC assignment. After going through some topics in this forum I have decided to start my assignment by reading data from the db.db file, so that I start gaining the understanding of the Data class and format etc. So I started with looking at RandomAccessFile API and created one to read from it. RandomAccessFile raf = new RandomAccessFile("db-2x3.db","r"); int magicCookie = raf.readInt(); short numberOfFields = raf.readShort(); When reading int, short, byte I have no issues. when I read data into byte array like in the following code... And for each field byte fieldNameLength = raf.readByte(); byte fieldName[] = new byte[fieldNameLength]; int n = raf.read(fieldName); My question is when I use the read(byte []) API, what is the best way to extract data from the byte array? Is there any other API that I am missing to see? I am reading each record into a byte array. Am I using the right API? If so what is the best way to parse that record data which is in byte array? Do I have write some byte array to String conversion methods. byte[] record = new byte[recordLength-1]; int n= raf.read(record); This is my first post in this forum. I would appreciate your help. Thanks, Java Tatpat
|
 |
Jacques Bosch
Ranch Hand
Joined: Dec 18, 2003
Posts: 319
|
|
Yes sure. Welcome Try byte fieldNameLength = raf.readByte(); byte fieldName[] = new byte[fieldNameLength]; int n = raf.read(fieldName); String strField = new String(fieldName); , "US-ASCII");
|
Jacques<br />*******<br />MCP, SCJP, SCJD, SCWCD
|
 |
Jacques Bosch
Ranch Hand
Joined: Dec 18, 2003
Posts: 319
|
|
Sorry. That was supposed to be. String strField = new String(fieldName, "US-ASCII");
|
 |
Murali Kurukunda
Ranch Hand
Joined: Oct 20, 2003
Posts: 36
|
|
|
Thanks!
|
 |
Murali Kurukunda
Ranch Hand
Joined: Oct 20, 2003
Posts: 36
|
|
Hi Jacques and others, Thanks for the previous replies..Now I have an understanding of reading from the RandomAccessFiles. I am looking at writing to it now. I have the record elements in the form of a String array. First I am trying to convert each element to byte array using getBytes("US-ASCII") method and then writing that array to the file using raf.write(byte []) method. For some reason I am not getting the expected result. DO you think it is correct way to do or please correct me if I am wrong. I appreciate your help. Thanks, Murali
|
 |
Andrew Monkhouse
author and jackaroo
Marshal Commander
Joined: Mar 28, 2003
Posts: 10895
|
|
Hi Murali, Welcome to JavaRanch. We don't have many rules here, but one we do have is the JavaRanch Official policy on registered names. We need you to display both a first and a last name. Could you please change your displayed name to meet this policy? You can change your displayed name here. You haven't said what is happening that was unexpected, which makes it a bit hard to guess what is wrong. Are you padding the byte array to the correct size for each field? Are you counting the position of the deleted flag? Regards, Andrew
|
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
|
 |
 |
|
|
subject: NX: Reading from the db.db file
|
|
|