This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: Reading from the db.db file

 
Murali Kurukunda
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Bosch
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry. That was supposed to be.
String strField = new String(fieldName, "US-ASCII");
 
Murali Kurukunda
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
Murali Kurukunda
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic