hello everybody,
i have a problem with reading the header of the db-file of my
scjp assignment. first it reads the magic value, then the number of the
fields i a record, but when it comes to reading the name of the first
field, it reads a lot further than the this fields name. i am using
the db.readUTF() method. i think the problem is that db.readUTF()
reads the first two bytes of the field to find out its length, but in the db-file the length of the field name is only one byte long.
could you have a look at my code and maybe give me a hint?
thank you!
public Data(
String dbname) throws IOException {
File f = new File(dbname);
db = new RandomAccessFile(f, "rw");
magic = db.readInt();
System.out.println("magic: " + magic);
int nFields= db.readShort();
System.out.println("nFields: " + nFields);
description = new FieldInfo[nFields];
for (int i=0; i<nFields; i++) {
String name = db.readUTF();
System.out.println("name: " + name);
int length = db.readByte();
System.out.println("fieldlength: " + length);
description[i] = new FieldInfo(name, length);
recordLen += description[i].getLength();
}
the output from running the programm is:
magic: 259
nFields: 7
name: amelocation@sizesmokingratdate
owneDew Drop Inn Smallville 4 Y$210.00 2005/05/02 Elephant Inn Smallville 4 Y$160.00 2004/05/06 Splendide Smallville 4 N$140.00 2004/02/15 Grandview Whoville 6 N$200.00 2004/04/24 Dew Drop Inn Whoville 4 Y$80.00 2005/07/17 Pandemonium Metropolis 6 N$90.00 2004/03/20 Castle Metropolis
fieldlength 32